首页 > 代码库 > POJ 3207 Ikki's Story IV - Panda's Trick

POJ 3207 Ikki's Story IV - Panda's Trick

Ikki‘s Story IV - Panda‘s Trick

Time Limit: 1000ms
Memory Limit: 131072KB
This problem will be judged on PKU. Original ID: 3207
64-bit integer IO format: %lld      Java class name: Main
 

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 “<code>panda is telling the truth...” or “<code>the evil panda is lying again”.

Sample Input

4 20 13 2

Sample Output

panda is telling the truth...

Source

POJ Monthly--2007.03.04

 

解题:传说中的2-SAT...

已知一个圆上顺时针放着 n 个点,这 n 个点中有m对顶点之间有连线,连线要么在园外要么在圆内,每个点最多连接一条边,问是否存在一种连接情况满足所有的边都不相交。

分析:将每条边看成两个点 i ,i+m 分别表示边在内部和在外部, 如果两条边i,j的端点存在序号上的交叉,则这两对点之间的连线一个在外部一个在内部,即如果存在 i,则必存在 j+m ,如果存在 j ,则必存在 i+m, 如果存在 i+m,则必存在 j ,如果存在 j+m ,则必存在 i,建图的时候连的边为

 i -> j+m

       j -> i+m

      i+m -> j

      j+m -> i

     求出强连通分量并染色,判断是否存在冲突的情况即可。

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 1100;18 int u[maxn],v[maxn],head[maxn],tot,scc,cnt,n,m;19 int dfn[maxn],low[maxn],belong[maxn];20 bool instack[maxn];21 stack<int>stk;22 struct arc {23     int to,next;24     arc(int x = 0,int y = 0) {25         to = x;26         next = y;27     }28 };29 arc e[1000010];30 void add(int u,int v) {31     e[tot] = arc(v,head[u]);32     head[u] = tot++;33 }34 bool ok(int i,int j) {35     return u[i]<v[j]&&u[i]>u[j]&&v[i]>v[j]||v[i]>u[j]&&v[i]<v[j]&&u[i]<u[j];36 }37 void init() {38     tot = scc = cnt = 0;39     memset(head,-1,sizeof(head));40     for(int i = 0; i < m; i++)41         for(int j = i+1; j < m; j++)42             if(ok(i,j)) {43                 add(i,j+m);44                 add(j,i+m);45                 add(j+m,i);46                 add(i+m,j);47             }48     memset(dfn,0,sizeof(dfn));49     memset(instack,false,sizeof(instack));50     while(!stk.empty()) stk.pop();51 }52 void tarjan(int u){53     dfn[u] = low[u] = ++cnt;54     stk.push(u);55     instack[u] = true;56     for(int i = head[u]; ~i; i = e[i].next){57         if(!dfn[e[i].to]){58             tarjan(e[i].to);59             if(low[e[i].to] < low[u])60                 low[u] = low[e[i].to];61         }else if(instack[e[i].to] && dfn[e[i].to] < low[u])62             low[u] = dfn[e[i].to];63 64     }65     if(dfn[u] == low[u]){66         scc++;67         int now;68         do{69             now = stk.top();70             stk.pop();71             instack[now] = false;72             belong[now] = scc;73         }while(now != u);74     }75 }76 bool solve(){77     for(int i = 0,t = m<<1; i < t; i++)78         if(!dfn[i]) tarjan(i);79     for(int i = 0; i < m; i++)80         if(belong[i] == belong[i+m]) return false;81     return true;82 }83 int main() {84     while(~scanf("%d %d",&n,&m)){85         for(int i = 0; i < m; i++){86             scanf("%d %d",u+i,v+i);87             if(u[i] > v[i]) swap(u[i],v[i]);88         }89         init();90         solve()?puts("panda is telling the truth..."):puts("the evil panda is lying again");91     }92     return 0;93 }
View Code

 

POJ 3207 Ikki's Story IV - Panda's Trick