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

烈酒焚心 提交于 2020-02-21 04:26:39

题意/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


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

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

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

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

 

代码/Code

 

<span style="font-family:SimSun;"><strong>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]) and (ty[j]>ty[i]) or (ty[j]>tx[i]) and (ty[j]<ty[i]) and (tx[j]<tx[i]) then
        begin
          add(i+m,j);
          add(j,i+m);
          add(i,j+m);
          add(j+m,i);
        end;
end;

function min(t,k:longint):longint;
begin
  if t<k then exit(t);
  exit(k);
end;

procedure tarjan(o:longint);
var
  i,k,tmp:longint;
begin
  inc(num);
  dfn[o]:=num; low[o]:=num;
  inc(op);
  sta[op]:=o; v[o]:=true;
  i:=ls[o];
  while i<>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.
</strong></span>


 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!