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 ofsuch 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 circularborder: 0, 1, 2, …, n − 1. Evil panda claims that he isconnecting m pairs of points. To connect two points, liympanda either placesthe link entirely inside the circle or entirely outside the circle. Now liympandatells Ikki no two links touch inside/outside the circle, except on theboundary. He wants Ikki to figure out whether this is possible…
Despaired at the minesweeping game just played, Ikki is totally ata 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 twointegers: 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 ithwire. Every point will have at most one link.
Output
Output a line, either “panda is telling the truth...
”or “the evil panda is lyingagain
”.
Sample Input
4 2
0 1
3 2
Sample Output
panda is telling the truth...
题目大意:平面上,一个圆,圆的边上按顺时针放着n个点。现在要连m条边,比如a,b,那么a到b可以从圆的内部连接,也可以从圆的外部连接。给你的信息中,每个点最多只会连接的一条边。问能不能连接这m条边,使这些边都不相交。
分析:题意可能刚开始不是很好理解,比如1 5连边,2,6连边,由于点是顺序排列的,一画图就可以发现,这两条边必须一个从圆外面连,一个从内部连,否则就会相交。如果再加入3 7这条边,那么就必须相交了。
这样,就可以转化成标准的2-sat问题:
i->!j, 表示i画内部的话,j只能画外部,即!j
j->!i
I!->j{对称构图}
j!->i
然后就是2-sat算法了,tarjan一下,如果有i和i'同属于一个强联通,返回false,否则就成立。
题目标程:
const
maxn=2005;
maxm=4000005;
type
node=record
x,y,next:longint;
end;
var
n,m,e,cnt,d,num,tot:longint;
ls,dfn,low,belong,stack:array [1..maxn] oflongint;
g:array [1..maxm] of node;
instack:array [1..maxn] of boolean;
procedure add(x,y:longint);
begin
inc(e);
g[e].x:=x;
g[e].y:=y;
g[e].next:=ls[x];
ls[x]:=e;
end;
function min(x,y:longint):longint;
begin
ifx<y then exit(x)
else exit(y);
end;
procedure tarjan(i:longint);
var j,t:longint;
begin
inc(d);
dfn[i]:=d;
low[i]:=d;
inc(tot);
stack[tot]:=i;
t:=ls[i];
instack[i]:=true;
while t>0 do
begin
j:=g[t].y;
ifdfn[j]=0 then
begin
tarjan(j);
low[i]:=min(low[i],low[j]);
end
else
if instack[j] then
low[i]:=min(low[i],dfn[j]);
t:=g[t].next;
end;
iflow[i]=dfn[i] then
begin
inc(num);
repeat
j:=stack[tot];
instack[j]:=false;
dec(tot);
belong[j]:=num;
until i=j;
end;
end;
procedure solve;
vari:longint;
begin
d:=0; num:=0;
fillchar(dfn,sizeof(dfn),0);
fori:=1 to n*2 do
ifdfn[i]=0 then
tarjan(i);
end;
procedure output;
vari:longint;
begin
fori:=1 to n do
ifbelong[i]=belong[i+n] then
begin writeln('the evil panda is lying again'); halt; end;
writeln('panda is telling the truth...');
end;
procedure init;
var
i,j:longint;
x,y:array[1..1000]of longint;
begin
fillchar(x,sizeof(x),0);
fillchar(y,sizeof(y),0);
readln(m,n);
for i:=1 to n do
begin
read(x[i],y[i]);
inc(x[i]);
inc(y[i]);
if x[i]>y[i] then
begin
x[i]:=x[i] xor y[i];
y[i]:=x[i] xor y[i];
x[i]:=x[i] xor y[i];
End;
end;
fori:=1 to n-1 do
forj:=i+1 to n do
if((x[i]<=x[j])and(y[i]>=x[j])and(y[i]<=y[j]))
or((x[i]>=x[j])and(x[i]<=y[j])and(y[i]>=y[j])) then
begin
add(i,j+n);
add(j,i+n);
add(i+n,j);
add(j+n,i);
end;
end;
begin
init;
solve;
output;
end.