每条边有两种连法,在圆的外侧或圆的内侧. 对于有可能相交的两条边,不能在同一侧. 建图判断2sat
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 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 “ Sample Input 4 2 0 1 3 2 Sample Output panda is telling the truth... Source
POJ Monthly--2007.03.04, Ikki
|
[Submit] [Go Back] [Status] [Discuss]
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn=1100; int n,m; struct Interval { int l,r; }I[maxn]; bool cmp(Interval a,Interval b) { if(a.l!=b.l) return a.l<b.l; return a.r<b.r; } struct Edge { int to,next; }edge[maxn*maxn*3]; int Adj[maxn],Size; void init() { Size=0; memset(Adj,-1,sizeof(Adj)); } void Add_Edge(int u,int v) { edge[Size].to=v; edge[Size].next=Adj[u]; Adj[u]=Size++; } int DFN[maxn],Low[maxn],Instack[maxn],Belong[maxn]; int Stack[maxn],top,scc,Index; void tarjan(int u) { DFN[u]=Low[u]=++Index; Stack[top++]=u; Instack[u]=true; int v; for(int i=Adj[u];~i;i=edge[i].next) { v=edge[i].to; if(!DFN[v]) { tarjan(v); Low[u]=min(Low[u],Low[v]); } else if(Instack[v]) { Low[u]=min(Low[u],DFN[v]); } } if(DFN[u]==Low[u]) { scc++; do { v=Stack[--top]; Instack[v]=false; Belong[v]=scc; }while(v!=u); } } bool twosat(int n) { memset(DFN,0,sizeof(DFN)); memset(Instack,0,sizeof(Instack)); top=scc=Index=0; for(int i=0;i<2*n;i++) if(!DFN[i]) tarjan(i); for(int i=0;i<2*n;i+=2) { if(Belong[i]==Belong[i^1]) return false; } return true; } int main() { while(scanf("%d%d",&n,&m)!=EOF) { int a,b; init(); for(int i=0;i<m;i++) { scanf("%d%d",&a,&b); if(a>b) swap(a,b); I[i]=(Interval){a,b}; } sort(I,I+m,cmp); for(int i=0;i<m;i++) { for(int j=i+1;j<m;j++) { if(I[j].l>=I[i].r) break; if(I[j].l!=I[i].l&&I[j].r>I[i].r) ///线段i和线段j不能在同一侧 { Add_Edge(i*2,j*2+1); Add_Edge(i*2+1,j*2); Add_Edge(j*2,i*2+1); Add_Edge(j*2+1,i*2); } } } if(twosat(m)) puts("panda is telling the truth..."); else puts("the evil panda is lying again"); } return 0; }