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


看了图,就知道了要把每条边看成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>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值