Ikki's Story IV - Panda's Trick poj3207 tarjan+2-SAT

题意/Description:

    liympanda, one of Ikki’s friend, likes playing games with Ikki. Today after minesweeping with Ikki and winning so many times, he is tired of such easy games and wants to play another game with Ikki.

    liympanda has a magic circle and he puts it on a plane, there are n points on its boundary in circular border: 0, 1, 2, …, n − 1. Evil panda claims that he is connecting m pairs of points. To connect two points, liympanda either places the link entirely inside the circle or entirely outside the circle. Now liympanda tells Ikki no two links touch inside/outside the circle, except on the boundary. He wants Ikki to figure out whether this is possible…

    Despaired at the minesweeping game just played, Ikki is totally at a loss, so he decides to write a program to help him.

 

读入/Input

 

    The input contains exactly one test case.

    In the test case there will be a line consisting of of two integers: n and m (n ≤ 1,000, m ≤ 500). The following m lines each contain two integers ai and bi, which denote the endpoints of the ith wire. Every point will have at most one link.

 

输出/Output

    Output a line, either “panda is telling the truth...” or “the evil panda is lying again”.

 

题解/solution

Ikki's Story IV - Panda's Trick poj3207 tarjan+2-SAT_第1张图片

看了图,就知道了要把每条边看成2个点:分别表示在内部连接和在外部连接,只能选择一个。计作点i和点i'。然后思考一下怎么连边:

1、i->j'表示如果i在里,j'在外

2、j->i' , i'->j , j'->i 同理

之后用2-SAT做,用tarjan找i和i'是不是强连通,如果不是,则成立。

 

代码/Code

 

const
  maxE=10000001;
  maxV=100001;
type
  arr=record
    x,y,next:longint;
  end;
var
  n,m,nm,op,num:longint;
  tu:array [0..maxE] of arr;
  v:array [0..maxE] of boolean;
  ls,tx,ty,sta,bel,dfn,low:array [0..maxV] of longint;
procedure add(o,p:longint);
begin
  inc(nm);
  with tu[nm] do
    begin
      x:=o; y:=p;
      next:=ls[o];
      ls[o]:=nm;
    end;
end;

procedure init;
var
  i,j,k:longint;
begin
  readln(n,m);
  for i:=1 to m do
    begin
      readln(tx[i],ty[i]);
      if tx[i]>ty[i] then
        begin
          k:=tx[i]; tx[i]:=ty[i]; ty[i]:=k;
        end;
    end;
  nm:=0;
  for i:=1 to m do
    for j:=i+1 to m do
      if (tx[j]>tx[i]) and(tx[j]ty[i]) or (ty[j]>tx[i]) and (ty[j]0 do
    begin
      if dfn[tu[i].y]=0 then
        begin
          tarjan(tu[i].y);
          low[o]:=min(low[o],low[tu[i].y]);
        end else
          if v[tu[i].y] then low[o]:=min(low[o],dfn[tu[i].y]);
      i:=tu[i].next;
    end;
  if dfn[o]=low[o] then
    begin
      inc(k);
      tmp:=-1;
      while tmp<>o do
        begin
          tmp:=sta[op];
          dec(op);
          bel[tmp]:=k;
          v[tmp]:=false;
        end;
    end;
end;

procedure print;
var
  i:longint;
  boo:boolean;
begin
  for i:=1 to m+m do
    if dfn[i]=0 then tarjan(i);
  boo:=true;
  for i:=1 to m do
    if bel[i]=bel[i+m] then boo:=false;
  if boo then write('panda is telling the truth...')
         else write('the evil panda is lying again');
end;

begin
  init;
  print;
end.


 

你可能感兴趣的:(Ikki's Story IV - Panda's Trick poj3207 tarjan+2-SAT)