【POJ3207】熊猫的戏法Ikki's Story IV - Panda'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 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.
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.
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
0 1
3 2
Sample Output
panda is telling the truth...
Solution
题目大意是说给出一个有n个编号为0~n-1的点的圆环,并且给出m对边关系,并且每一
条边可以出现在圆环内部或者是外部。现在要求判断这m条边是否能够不相交。
根据题目描述,每一条边只有可能存在两种状态:在圆的内部和圆的外部。每条边可视
作一个有两种状态的集合,并且对于不同集合不能够出现同一状态。这好像是一个标准的2
-Sat模型。
对于两条如果是同一状态就会相交的线段i和j,连边(i,j'),(i',j),(j,i'),(j',i)。
对于两条边,会相交当且仅当
line[x].st<line[y].st&&line[x].ed>line[y].st&&line[x].ed<line[y].ed
或者line[y].st<line[x].st&&line[y].ed>line[x].st&&line[y].ed<line[x].ed
//并没有使用tarjan。。。
条边可以出现在圆环内部或者是外部。现在要求判断这m条边是否能够不相交。
根据题目描述,每一条边只有可能存在两种状态:在圆的内部和圆的外部。每条边可视
作一个有两种状态的集合,并且对于不同集合不能够出现同一状态。这好像是一个标准的2
-Sat模型。
对于两条如果是同一状态就会相交的线段i和j,连边(i,j'),(i',j),(j,i'),(j',i)。
对于两条边,会相交当且仅当
line[x].st<line[y].st&&line[x].ed>line[y].st&&line[x].ed<line[y].ed
或者line[y].st<line[x].st&&line[y].ed>line[x].st&&line[y].ed<line[x].ed
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
inline int read(){
char c;int rec=0;
while((c=getchar())<'0'|c>'9');
while(c>='0'&&c<='9')rec=rec*10+c-'0',c=getchar();
return rec;
}
int n,m;
struct Line{int st,ed;}line[5005];
inline bool Check(int x,int y){
if(line[x].st<line[y].st&&line[x].ed>line[y].st&&line[x].ed<line[y].ed
||line[y].st<line[x].st&&line[y].ed>line[x].st&&line[y].ed<line[x].ed)
return 1;return 0;
}
struct Branch {int next,to;}f[500005],g[500005];
int h[50005],e[50005],cnt=0;
inline void add(int x,int y){
f[++cnt].to=y;f[cnt].next=h[x];h[x]=cnt;
g[cnt].to=x;g[cnt].next=e[y];e[y]=cnt;
return ;
}
int tot,group[50005];
int vis[50005],a[50005],b[50005];
inline void DfsF(int v){
vis[v]=1;
for(int i=h[v];i;i=f[i].next){
int j=f[i].to;
if(!vis[j])DfsF(j);
}a[++tot]=v;return ;
}
inline void DfsG(int v){
vis[v]=0;group[v]=tot;
for(int i=e[v];i;i=g[i].next){
int j=g[i].to;
if(vis[j]==1)DfsG(j);
}return ;
}
int main(){
n=read();m=read();
for(int i=1;i<=m;i++){
line[i].st=read()+1;
line[i].ed=read()+1;
if(line[i].st>line[i].ed)swap(line[i].st,line[i].ed);
}
for(int i=1;i<=m;i++){
for(int j=i+1;j<=m;j++){
if(Check(i,j)){
add(i,j+m);add(j,i+m);
add(i+m,j);add(j+m,i);
}
}
}
for(int i=1;i<=m<<1;i++)
if(!vis[i])DfsF(i);
tot=0;
for(int i=m<<1;i>=1;i--)
if(vis[a[i]]==1)
b[++tot]=a[i],DfsG(a[i]);
for(int i=1;i<=m;i++)
if(group[i]==group[i+m])
{cout<<"the evil panda is lying again";return 0;}
cout<<"panda is telling the truth...";
return 0;
}
//并没有使用tarjan。。。