POJ3207 Ikki's Story IV – Panda's Trick

Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 9426   Accepted: 3465

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

POJ Monthly--2007.03.04, Ikki

又是WA在数组不够大←

连接两个点的一段线,要么全部在圆里,要么全部在圆外

先在各段线之间连边:若是两段线可能有交叉,那么必须一个在圆里,一个在圆外

建好图后用tarjan求强连通分量,若是表示一段线在圆里和圆外的两个点在同一个强连通分量里,那么就不可行

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 const int mxn=600000;
 8 //bas
 9 int n,m;
10 int hd[mxn],cnt=0;
11 int a[mxn],b[mxn];
12
13 //edge
14 struct edge{
15     int to;
16     int next;
17 }e[mxn];
18 void add_edge(int u,int v){
19     e[++cnt].next=hd[u];e[cnt].to=v;hd[u]=cnt;
20 }
21
22 //tarjan
23 int vis[mxn];
24 int dfn[mxn],low[mxn];
25 int st[mxn],top;
26 bool inst[mxn];
27 int dtime=0;
28 int belone[mxn],tot;
29 void tarjan(int s){
30     low[s]=dfn[s]=++dtime;
31     st[++top]=s;
32     inst[s]=1;
33     for(int i=hd[s];i;i=e[i].next){
34         int v=e[i].to;
35         if(!dfn[v]){
36             tarjan(v);
37             low[s]=min(low[s],low[v]);
38         }
39         else if(inst[v]){
40             low[s]=min(low[s],dfn[v]);
41         }
42     }
43     int v;
44     if(low[s]==dfn[s]){
45         cnt++;
46         do{
47             v=st[top--];
48             inst[v]=0;
49             belone[v]=cnt;
50
51         }while(v!=s);
52     }
53     return;
54 }
55
56 //
57 void Build(){
58     int i,j;
59     for(i=1;i<m;i++)
60       for(j=i+1;j<=m;j++){
61           if((a[i]<a[j] && a[j]<b[i] && b[i]<b[j]) ||
62            (a[i]>a[j] && a[i]<b[j] && b[i]>b[j])    ){
63                    add_edge(i,j+m);//用+m的点表示在外面
64                    add_edge(j,i+m);
65                    add_edge(i+m,j);
66                    add_edge(j+m,i);
67
68            }
69       }
70     return;
71 }
72 int main(){
73     scanf("%d%d",&n,&m);
74     int i,j;
75     for(i=1;i<=m;i++){
76         scanf("%d%d",&a[i],&b[i]);
77         a[i]++;b[i]++;
78         if(a[i]>b[i])swap(a[i],b[i]);
79     }
80     Build();
81     n=2*m;
82     for(i=1;i<=n;i++)if(!dfn[i])tarjan(i);
83     for(i=1;i<=m;i++){
84         if(belone[i]==belone[i+m]){
85             printf("the evil panda is lying again");
86             return 0;
87         }
88     }
89     printf("panda is telling the truth...");
90     return 0;
91 }

POJ3207 Ikki's Story IV – Panda's Trick

时间: 2024-11-03 20:59:38

POJ3207 Ikki's Story IV – Panda's Trick的相关文章

poj3207 Ikki&#39;s Story IV - Panda&#39;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 put

POJ3207 Ikki&#39;s Story IV - Panda&#39;s Trick 【2-sat】

题目 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

POJ Ikki&#39;s Story IV - Panda&#39;s Trick [2-SAT]

题意: 圆上n个点,m对点之间连边,连在园内或园外,所有边不相交是否可行 发现两对点连线都在内相交则都在外也相交,那么只有一个在内一个在外啦,转化为$2-SAT$问题 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const int N=1005,M=5e5; t

HDU 3207 Ikki&#39;s Story IV - Panda&#39;s Trick(图论-2SAT,图论-tarjan)

Ikki's Story IV - Panda's Trick Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7821   Accepted: 2892 Description liympanda, one of Ikki's friend, likes playing games with Ikki. Today after minesweeping with Ikki and winning so many tim

POJ 3207 Ikki&#39;s Story IV - Panda&#39;s Trick

简单的看了2-sat……似乎还是挺神奇的东西……等大致刷完几道题再来写总结吧! 而这道题……算是2-sat的超级入门题了吧 不过题目大意也是醉了:圆上顺序排列n个点,现要在一些点间连边,规定边只能在圆内或圆外,求有没有可能不相交 .一开始想的是嗷嗷嗷,圆上两个点的连线怎么可能有什么在圆外在圆内之分,不都是弦么?后来才知道……原来两个点的连线不是直线可以使曲线…… 判定是否相交很简单吧……看成是一条直线上四个点,那么如果e1.a<e2.a<e1.b<e2.b就相交啦…… 都说是热身题没多难

POJ 3207 Ikki&#39;s Story IV - Panda&#39;s Trick (2-SAT)

题目地址:POJ 3207 找好矛盾关系,矛盾关系是(2,5)和(3,6)这两个只能一个在外边,一个在里边,利用这个矛盾关系来建图. 可以用在外边和里边来当1和0,最后判断每对是否出现矛盾. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #inc

POJ 3207 Ikki&#39;s Story IV - Panda&#39;s Trick(2 - sat啊)

题目链接:http://poj.org/problem?id=3207 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. liy

【POJ】3207 Ikki&#39;s Story IV - Panda&#39;s Trick

http://poj.org/problem?id=3207 题意:一个圆上顺时针依次排列着标号为1-n的点,这些点之间共有m条边相连,每两个点只能在圆内或者圆外连边.问是否存在这些边不相交的方案.(n<=1000, m<=500) #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <iostream> using n

POJ 3207 Ikki&#39;s Story IV - Panda&#39;s Trick(2-sat)

POJ 3207 Ikki's Story IV - Panda's Trick 题目链接 题意:一个圆上顺序n个点,然后有m组连线,连接两点,要求这两点可以往圆内或圆外,问是否能构造出使得满足所有线段不相交 思路:2-sat,判断相交的建边,一个在内,一个在外,然后跑一下2-sat即可 代码: #include <cstdio> #include <cstring> #include <cstdlib> #include <vector> #include