poj3207 Ikki's Story IV - Panda's Trick
Posted zbtrs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj3207 Ikki's Story IV - Panda's Trick相关的知识,希望对你有一定的参考价值。
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 10448 | Accepted: 3829 |
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
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <stack> using namespace std; const int maxn = 1000010; int n,m,a[maxn],b[maxn],pre[maxn],scc[maxn],top,low[maxn],dfs_clock,head[maxn],nextt[maxn],to[maxn],tot = 1; void add(int x,int y) { to[tot] = y; nextt[tot] = head[x]; head[x] = tot++; } stack <int> s; void tarjan(int u) { low[u] = pre[u] = ++dfs_clock; s.push(u); for (int i = head[u];i;i = nextt[i]) { int v = to[i]; if (!pre[v]) { tarjan(v); low[u] = min(low[u],low[v]); } else if (!scc[v]) low[u] = min(low[u],pre[v]); } if (low[u] == pre[u]) { top++; while (1) { int t = s.top(); s.pop(); scc[t] = top; if (t == u) break; } } } int main() { scanf("%d%d",&n,&m); for (int i = 1; i <= m; i++) { scanf("%d%d",&a[i],&b[i]); if (a[i] > b[i]) swap(a[i],b[i]); } for (int i = 1; i <= m; i++) for (int j = i + 1; j <= m; j++) if (a[ i ] < a[ j ] && b[ i ] > a[ j ] && b[ i ] < b[ j ] || b[ i ] > b[ j ] && a[ i ] > a[ j ] && a[ i ] < b[ j ]) { add(i * 2,j * 2 + 1); add(j * 2 + 1,i * 2); add(i * 2 + 1,j * 2); add(j * 2,i * 2 + 1); } for (int i = 2; i <= m * 2 + 1; i++) if (!pre[i]) tarjan(i); for (int i = 2; i <= m * 2; i+=2) if (scc[i] == scc[i + 1]) { printf( "the evil panda is lying again" ); return 0; } printf("panda is telling the truth..." ); return 0; }
以上是关于poj3207 Ikki's Story IV - Panda's Trick的主要内容,如果未能解决你的问题,请参考以下文章
POJ3207 Ikki's Story IV - Panda's Trick 2-sat
[2-SAT] poj 3207 Ikki's Story IV - Panda's Trick
POJ 3207 Ikki's Story IV - Panda's Trick
POJ 3207 Ikki's Story IV - Panda's Trick(2-sat)