「POJ3207」Ikki’s Story IV – Panda’s Trick
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 integersai 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
1 2 3 |
4 2 0 1 3 2 |
Sample Output
1 |
panda is telling the truth... |
Source
http://blog.csdn.net/jarjingx/article/details/8521690
关于2-sat,这篇博文非常赞
这题若两条边在圆内相交,则他们在圆外也是相交的,即若a,b不能同时取,a’,b’也不能同时取
按2-sat建模缩点后判断合法性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cmath> #include<cstring> #define inf 2000000000 #define ll long long #define mod 1000000007 using namespace std; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,m,ind,top,cnt,scc; int a[505],b[505],last[2005]; int dfn[1005],low[1005],q[1005],belong[1005]; bool inq[1005]; struct data{int to,next,v;}e[500005]; bool jud() { for(int i=1;i<=m;i++) if(belong[2*i]==belong[2*i-1])return 0; return 1; } void insert(int u,int v) { e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt; } void tarjan(int x) { low[x]=dfn[x]=++ind; q[++top]=x;inq[x]=1; for(int i=last[x];i;i=e[i].next) if(!dfn[e[i].to]) { tarjan(e[i].to); low[x]=min(low[x],low[e[i].to]); } else if(inq[e[i].to]) low[x]=min(low[x],dfn[e[i].to]); if(low[x]==dfn[x]) { scc++; int now=0; while(now!=x) { now=q[top--]; belong[now]=scc; inq[now]=0; } } } void build() { for(int i=1;i<=m;i++) for(int j=i+1;j<=m;j++) if((a[i]<a[j]&&a[j]<b[i]&&b[j]>b[i])||(a[j]<a[i]&&a[i]<b[j]&&b[i]>b[j])) { insert(2*i-1,2*j); insert(2*j,2*i-1); insert(2*j-1,2*i); insert(2*i,2*j-1); } } int main() { n=read();m=read(); for(int i=1;i<=m;i++) { a[i]=read(),b[i]=read(); if(a[i]>b[i])swap(a[i],b[i]); } build(); for(int i=1;i<=2*m;i++) if(!dfn[i])tarjan(i); if(jud())puts("panda is telling the truth..."); else puts("the evil panda is lying again"); return 0; } |