poj3207 Ikki's Story IV - Panda's Trick 2011-12-26
Ikki's Story IV - Panda's TrickTime Limit: 1000MSMemory Limit: 131072KTotal Submissions: 4996Accepted: 1843
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”.
Sample Input
4 20 13 2
Sample Output
panda is telling the truth...
Source
POJ Monthly--2007.03.04, Ikki _______________________________________
1 Program Stone; 2 var i,j,k,l,le,deep,n,m,o:longint; 3 a,b:array[1..500]of longint; 4 head,next,date,tr:array[1..1000000]of longint; 5 stack,dfn,low:array[1..1000000]of longint; 6 v,f:array[1..1000000]of boolean; 7 procedure add(x,y:longint); 8 begin 9 inc(le); 10 date[le]:=y; 11 next[le]:=head[x]; 12 head[x]:=le; 13 end; 14 function check(x,y,z:longint):boolean; 15 begin 16 if (x>y)and(x<z) then check:=true 17 else check:=false; 18 end; 19 Procedure init; 20 var i,j,k:longint; 21 begin 22 readln(n,m); 23 for i:=1 to m do 24 begin 25 readln(a[i],b[i]); 26 if a[i]>b[i] then begin k:=a[i];a[i]:=b[i];b[i]:=k;end; 27 for j:=1 to i-1 do 28 if check(a[i],a[j],b[j])xor check(b[i],a[j],b[j]) then 29 begin 30 add(i,j+m); 31 add(j,i+m); 32 add(i+m,j); 33 add(j+m,i); 34 end; 35 end; 36 le:=0; 37 end; 38 procedure work(x:longint); 39 var i,j,k:longint; 40 begin 41 inc(o); 42 repeat 43 tr[stack[le]]:=o; //染色 44 if stack[le]>m then i:=stack[le]-m else i:=stack[le]+m; 45 if tr[i]=o then begin //如果冲突则无解 46 write('the evil panda is lying again'); 47 halt; 48 end; 49 f[stack[le]]:=false; 50 dec(le); 51 until stack[le+1]=x; 52 end; 53 function min(a,b:longint):longint; 54 begin 55 if a<b then min:=a else min:=b; 56 end; 57 procedure tarjan(x:longint); 58 var i,j,k:longint; 59 begin 60 v[x]:=false; 61 inc(deep); 62 dfn[x]:=deep; 63 low[x]:=deep; 64 inc(le); 65 stack[le]:=x; 66 f[x]:=true; 67 i:=head[x]; 68 while i<>0 do 69 begin 70 if v[date[i]] then 71 begin 72 tarjan(date[i]); 73 low[x]:=min(low[x],low[date[i]]); 74 end 75 else if f[date[i]] then low[x]:=min(low[x],dfn[date[i]]); 76 i:=next[i]; 77 end; 78 if dfn[x]=low[x] then work(x); 79 end; 80 Begin 81 assign(input,'input.in');reset(input); 82 init; 83 fillchar(f,sizeof(f),false); 84 fillchar(v,sizeof(v),true); 85 for i:=1 to 2*m do 86 if v[i] then tarjan(i); 87 writeln('panda is telling the truth...'); 88 end.