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】:
  
 /**
Status:Accepted Memory:18024K
Time:219MS Language:G++
Code Lenght:2100B Author:cj
*/ #include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack> #define N 1010000 //RE很多次,一个个试出来的
using namespace std; struct Edge
{
int next,v;
}edge[N]; int a[N],b[N]; int n,m,edge_cnt;
int head[N]; int pre[N],low_link[N],sccno[N],dfs_cnt,scc_cnt;
stack<int> stk; void addEdge(int u,int v)
{
edge[edge_cnt].v = v;
edge[edge_cnt].next = head[u];
head[u] = edge_cnt++;
} void Tarjan(int u)
{
pre[u]=low_link[u] = ++dfs_cnt;
stk.push(u);
int i;
for(i=head[u];i!=-;i=edge[i].next)
{
int v = edge[i].v;
if(!pre[v])
{
Tarjan(v);
low_link[u]=min(low_link[u],low_link[v]);
}
else if(!sccno[v])
{
low_link[u] = min(low_link[u],pre[v]);
}
}
if(pre[u]==low_link[u])
{
scc_cnt++;
int x;
do
{
x = stk.top();
stk.pop();
sccno[x] = scc_cnt;
}while(x!=u);
}
} void find_scc()
{
int i;
memset(pre,,sizeof(pre));
memset(low_link,,sizeof(low_link));
memset(sccno,,sizeof(sccno));
dfs_cnt = scc_cnt = ;
for(i=;i<=*m;i++)
{
if(!pre[i]) Tarjan(i);
}
} int main()
{
scanf("%d%d",&n,&m);
int i;
for(i=;i<=m;i++)
{
scanf("%d%d",&a[i],&b[i]);
if(a[i]>b[i]) swap(a[i],b[i]);
}
int j;
memset(head,-,sizeof(head));
edge_cnt = ;
for(i=;i<=m;i++)
{
for(j=;j<=m;j++)
{
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]))
{
addEdge(i,j+m);
addEdge(j,i+m);
addEdge(i+m,j);
addEdge(j+m,i);
}
}
}
find_scc();
for(i=;i<=m;i++)
if(sccno[i]==sccno[i+m])
{
break;
}
if(i<=m) puts("the evil panda is lying again");
else puts("panda is telling the truth...");
return ;
}
  
上一篇:POJ 3207 Ikki's Story IV - Panda's Trick (2-SAT)


下一篇:POJ 3207 Ikki's Story IV - Panda's Trick(2-sat问题)