poj 3207 Ikki's Story IV - Panda's Trick (2-SAT)

http://poj.org/problem?id=3207

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

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

 
【题解】:第一个2-SAT题,不懂觉厉
  题目大意:圆上顺时针给0到n-1个数字,数字之间用线连起来(可以是曲线),能不能保证不相交
 
【code】:
  
  1 /**
  2 Status:Accepted        Memory:18024K    
  3 Time:219MS        Language:G++    
  4 Code Lenght:2100B   Author:cj
  5 */
  6 
  7 #include<iostream>
  8 #include<stdio.h>
  9 #include<string.h>
 10 #include<algorithm>
 11 #include<stack>
 12 
 13 #define N 1010000  //RE很多次,一个个试出来的
 14 using namespace std;
 15 
 16 struct Edge
 17 {
 18     int next,v;
 19 }edge[N];
 20 
 21 int a[N],b[N];
 22 
 23 int n,m,edge_cnt;
 24 int head[N];
 25 
 26 int pre[N],low_link[N],sccno[N],dfs_cnt,scc_cnt;
 27 stack<int> stk;
 28 
 29 void addEdge(int u,int v)
 30 {
 31     edge[edge_cnt].v = v;
 32     edge[edge_cnt].next = head[u];
 33     head[u] = edge_cnt++;
 34 }
 35 
 36 void Tarjan(int u)
 37 {
 38     pre[u]=low_link[u] = ++dfs_cnt;
 39     stk.push(u);
 40     int i;
 41     for(i=head[u];i!=-1;i=edge[i].next)
 42     {
 43         int v = edge[i].v;
 44         if(!pre[v])
 45         {
 46             Tarjan(v);
 47             low_link[u]=min(low_link[u],low_link[v]);
 48         }
 49         else if(!sccno[v])
 50         {
 51             low_link[u] = min(low_link[u],pre[v]);
 52         }
 53     }
 54     if(pre[u]==low_link[u])
 55     {
 56         scc_cnt++;
 57         int x;
 58         do
 59         {
 60             x = stk.top();
 61             stk.pop();
 62             sccno[x] = scc_cnt;
 63         }while(x!=u);
 64     }
 65 }
 66 
 67 void find_scc()
 68 {
 69     int i;
 70     memset(pre,0,sizeof(pre));
 71     memset(low_link,0,sizeof(low_link));
 72     memset(sccno,0,sizeof(sccno));
 73     dfs_cnt = scc_cnt = 0;
 74     for(i=1;i<=2*m;i++)
 75     {
 76         if(!pre[i]) Tarjan(i);
 77     }
 78 }
 79 
 80 int main()
 81 {
 82     scanf("%d%d",&n,&m);
 83     int i;
 84     for(i=1;i<=m;i++)
 85     {
 86         scanf("%d%d",&a[i],&b[i]);
 87         if(a[i]>b[i])   swap(a[i],b[i]);
 88     }
 89     int j;
 90     memset(head,-1,sizeof(head));
 91     edge_cnt = 0;
 92     for(i=1;i<=m;i++)
 93     {
 94         for(j=1;j<=m;j++)
 95         {
 96             if((a[i]<a[j]&&b[i]<b[j]&&b[i]>a[j])||(a[i]>a[j]&&b[i]>b[j]&&b[i]<a[j]))
 97             {
 98                 addEdge(i,j+m);
 99                 addEdge(j,i+m);
100                 addEdge(i+m,j);
101                 addEdge(j+m,i);
102             }
103         }
104     }
105     find_scc();
106     for(i=1;i<=m;i++)
107         if(sccno[i]==sccno[i+m])
108         {
109             break;
110         }
111     if(i<=m) puts("the evil panda is lying again");
112     else    puts("panda is telling the truth...");
113     return 0;
114 }

 

  

转载于:https://www.cnblogs.com/crazyapple/p/3272294.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值