http://poj.org/problem?id=3207
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 7021 | Accepted: 2604 |
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 2 0 1 3 2
Sample Output
panda is telling the truth...
Source
1 /** 2 Status:Accepted Memory:18024K 3 Time:219MS Language:G++ 4 Code Lenght:2100B Author:cj 5 */ 6 7 #include<iostream> 8 #include<stdio.h> 9 #include<string.h> 10 #include<algorithm> 11 #include<stack> 12 13 #define N 1010000 //RE很多次,一个个试出来的 14 using namespace std; 15 16 struct Edge 17 { 18 int next,v; 19 }edge[N]; 20 21 int a[N],b[N]; 22 23 int n,m,edge_cnt; 24 int head[N]; 25 26 int pre[N],low_link[N],sccno[N],dfs_cnt,scc_cnt; 27 stack<int> stk; 28 29 void addEdge(int u,int v) 30 { 31 edge[edge_cnt].v = v; 32 edge[edge_cnt].next = head[u]; 33 head[u] = edge_cnt++; 34 } 35 36 void Tarjan(int u) 37 { 38 pre[u]=low_link[u] = ++dfs_cnt; 39 stk.push(u); 40 int i; 41 for(i=head[u];i!=-1;i=edge[i].next) 42 { 43 int v = edge[i].v; 44 if(!pre[v]) 45 { 46 Tarjan(v); 47 low_link[u]=min(low_link[u],low_link[v]); 48 } 49 else if(!sccno[v]) 50 { 51 low_link[u] = min(low_link[u],pre[v]); 52 } 53 } 54 if(pre[u]==low_link[u]) 55 { 56 scc_cnt++; 57 int x; 58 do 59 { 60 x = stk.top(); 61 stk.pop(); 62 sccno[x] = scc_cnt; 63 }while(x!=u); 64 } 65 } 66 67 void find_scc() 68 { 69 int i; 70 memset(pre,0,sizeof(pre)); 71 memset(low_link,0,sizeof(low_link)); 72 memset(sccno,0,sizeof(sccno)); 73 dfs_cnt = scc_cnt = 0; 74 for(i=1;i<=2*m;i++) 75 { 76 if(!pre[i]) Tarjan(i); 77 } 78 } 79 80 int main() 81 { 82 scanf("%d%d",&n,&m); 83 int i; 84 for(i=1;i<=m;i++) 85 { 86 scanf("%d%d",&a[i],&b[i]); 87 if(a[i]>b[i]) swap(a[i],b[i]); 88 } 89 int j; 90 memset(head,-1,sizeof(head)); 91 edge_cnt = 0; 92 for(i=1;i<=m;i++) 93 { 94 for(j=1;j<=m;j++) 95 { 96 if((a[i]<a[j]&&b[i]<b[j]&&b[i]>a[j])||(a[i]>a[j]&&b[i]>b[j]&&b[i]<a[j])) 97 { 98 addEdge(i,j+m); 99 addEdge(j,i+m); 100 addEdge(i+m,j); 101 addEdge(j+m,i); 102 } 103 } 104 } 105 find_scc(); 106 for(i=1;i<=m;i++) 107 if(sccno[i]==sccno[i+m]) 108 { 109 break; 110 } 111 if(i<=m) puts("the evil panda is lying again"); 112 else puts("panda is telling the truth..."); 113 return 0; 114 }