Poj 3207-Ikki's Story IV - Panda's Trick 2-sat

Ikki's Story IV - Panda's Trick
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 9183 Accepted: 3390

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

题意:一个圆上依次按顺时针分布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 }
View Code

 

转载于:https://www.cnblogs.com/Var123/p/5311310.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值