首页 > 代码库 > poj 3207 Ikki's Story IV - Panda's Trick
poj 3207 Ikki's Story IV - Panda's Trick
圆上依次从0到n-1标记点,给出m个边,问当这m个边连好之后,是否有相交的
边有园外园内之分……
2-SAT
#include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<algorithm> using namespace std; const int MAXN = 510; struct twosat { int n,c; vector<int> g[MAXN<<1]; bool mark[MAXN<<1]; int s[MAXN<<1]; bool dfs(int x) { int i; if (mark[x^1]) return 0; if (mark[x]) return 1; mark[x] = 1; s[c++] = x; for (i = 0; i < g[x].size(); i++) if (!dfs(g[x][i])) return 0; return 1; } void init(int n) { int i,t=n<<1; this->n = n; for (i = 0; i < t; i++) g[i].clear(); memset(mark, 0, sizeof(mark)); } void add_clause(int x, int xval, int y, int yval) { x = x * 2 + xval; y = y * 2 + yval; g[x^1].push_back(y); g[y^1].push_back(x); } bool solve() { int i,t=n<<1; for (i = 0; i < t; i += 2) if (!mark[i] && !mark[i + 1]) { c = 0; if (!dfs(i)) { while (c > 0) mark[s[--c]] =0; if (!dfs(i + 1)) return 0; } } return 1; } }ender; struct seg { int s,e; }in[510]; bool iscross(seg a,seg b,int flag1,int flag2) { if(flag1!=flag2) return 0; if(a.s<b.s&&b.s<a.e&&(b.e<a.s||b.e>a.e)) return 1; if(b.s<a.s&&a.s<b.e&&(a.e<b.s||a.e>b.e)) return 1; return 0; } int main() { int i,j,n,m,x,y; cin>>n>>m; for(i=0;i<m;i++) { cin>>in[i].s>>in[i].e; if(in[i].s>in[i].e) swap(in[i].s,in[i].e); } ender.init(m); for(i=0;i<m;i++) for(j=i+1;j<m;j++) for(x=0;x<2;x++) for(y=0;y<2;y++) if(iscross(in[i],in[j],x,y)) ender.add_clause(i,x,j,y); if(ender.solve()) cout<<"panda is telling the truth..."<<endl; else cout<<"the evil panda is lying again"<<endl; return 0; }
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 8142 | Accepted: 2993 |
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 andm (n ≤ 1,000, m ≤ 500). The following m lines each contain two integersai and bi, which denote the endpoints of theith 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
poj 3207 Ikki's Story IV - Panda's Trick