首页 > 代码库 > 【POJ 3207】Ikki's Story IV - Panda's Trick
【POJ 3207】Ikki's Story IV - Panda's Trick
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 8175 | Accepted: 3011 |
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
2-SAT判断可行性模板题。
题意是:
给出一个圆,给出m个点,点与点之间有连线,连线可在圆内或圆外、可直可弯曲。问这些线段能否不想交。
连线在圆内或圆外,对于有两种选择的问题,考虑2-SAT。
不能在同时在圆内或圆外的情况:
两个点与另外两个点的连线同在圆内或同在圆外时会交叉
#include <iostream> #include <cstdio> #include <algorithm> #include <cstdlib> #include <cmath> #include <stack> #include <cstring> using namespace std; int tot=0,n,m,a[5000],b[5000],h[5000],pre[5000],lowlink[5000],dfs_clock=0,scc_cnt=0,sccno[5000]; stack<int> s; struct edge { int y,ne; }e[1000000]; void Add(int x,int y) { tot++; e[tot].ne=h[x]; e[tot].y=y; h[x]=tot; } void dfs(int u) { pre[u]=lowlink[u]=++dfs_clock; s.push(u); for (int i=h[u];i;i=e[i].ne) { int v=e[i].y; if (!pre[v]) { dfs(v); lowlink[u]=min(lowlink[v],lowlink[u]); } else if (!sccno[v]) lowlink[u]=min(lowlink[u],pre[v]); } if (lowlink[u]==pre[u]) { scc_cnt++; for (;;) { int x=s.top(); s.pop(); sccno[x]=scc_cnt; if (x==u) break; } } } void Findscc() { dfs_clock=scc_cnt=0; memset(pre,0,sizeof(pre)); memset(sccno,0,sizeof(sccno)); for (int i=1;i<=2*m;i++) if (!pre[i]) dfs(i); } 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]<b[j]&&b[i]>a[j])||(a[i]>a[j]&&b[i]>b[j]&&a[i]<b[j])) //不能同时在圆内或圆外 { Add(i,j+m); Add(i+m,j); Add(j,i+m); Add(j+m,i); } Findscc(); for (int i=1;i<=m;i++) if (sccno[i]==sccno[i+m]) { printf("the evil panda is lying again\n"); return 0; } printf("panda is telling the truth...\n"); return 0; }
感悟:
1.CE是头文件忘了打<cstring>
RE是存边的数组开太小!!
2.2-SAT的问题关键在于发现二者选其一是哪二者,是哪些之间要二者选其一。
【POJ 3207】Ikki's Story IV - Panda's Trick