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
题意:一个圆上依次按顺时针分布0~n-1这些点。然后给出m条边,每条边可以从圆内或圆外连接。每个点只能连接一条边。问是否有可能的方案,使m条边不相交。
题解:
第一道 2-sat !!!
但还是没有搞太懂。。。QAQ
下面的这个好像更好懂。。。
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define MAXN 1010 4 #define MAXM 510 5 int SIZE,SC,belong[2*MAXM],a[MAXM],b[MAXM],LOW[2*MAXM],DFN[2*MAXM],STACK[2*MAXM],top; 6 bool INSTACK[2*MAXM]; 7 struct node 8 { 9 int begin,end,next; 10 }edge[MAXM*MAXM*4]; 11 int cnt,Head[2*MAXM]; 12 void addedge(int bb,int ee) 13 { 14 edge[++cnt].begin=bb;edge[cnt].end=ee;edge[cnt].next=Head[bb];Head[bb]=cnt; 15 } 16 void addedge1(int bb,int ee) 17 { 18 addedge(bb,ee);addedge(ee,bb); 19 } 20 void Tarjan(int u) 21 { 22 int i,v; 23 LOW[u]=DFN[u]=++SIZE; 24 STACK[top++]=u;INSTACK[u]=true; 25 for(i=Head[u];i!=-1;i=edge[i].next) 26 { 27 v=edge[i].end; 28 if(!DFN[v]) 29 { 30 Tarjan(v); 31 LOW[u]=min(LOW[u],LOW[v]); 32 } 33 else if(INSTACK[v]==true)LOW[u]=min(LOW[u],DFN[v]); 34 } 35 if(LOW[u]==DFN[u]) 36 { 37 SC++; 38 do 39 { 40 v=STACK[--top]; 41 INSTACK[v]=false; 42 belong[v]=SC; 43 }while(u!=v); 44 } 45 } 46 int main() 47 { 48 int n,m,i,j; 49 scanf("%d %d",&n,&m); 50 for(i=1;i<=m;i++) 51 { 52 scanf("%d %d",&a[i],&b[i]); 53 if(a[i]>b[i])swap(a[i],b[i]); 54 } 55 memset(Head,-1,sizeof(Head));cnt=1; 56 for(i=1;i<=m;i++) 57 { 58 for(j=i+1;j<=m;j++) 59 { 60 if((a[i]<a[j]&&a[j]<b[i]&&b[i]<b[j])||(a[j]<a[i]&&a[i]<b[j]&&b[j]<b[i])) 61 { 62 addedge1(2*i-1,2*j); 63 addedge1(2*j-1,2*i); 64 } 65 } 66 } 67 SIZE=0;SC=0; 68 for(i=1;i<=2*m;i++)if(!DFN[i])Tarjan(i); 69 for(i=1;i<=m;i++) 70 { 71 if(belong[2*i]==belong[2*i-1]){printf("the evil panda is lying again");return 0;} 72 } 73 printf("panda is telling the truth..."); 74 fclose(stdin); 75 fclose(stdout); 76 return 0; 77 }
|
转载于:https://www.cnblogs.com/Var123/p/5311310.html