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...
————————————————
分析:
仔细分析得知,不能相交的线组成一个2-SAT,所以使用kosaraju算法。->不会这个算法点我试试<-
注意线段相交的判断,由于题给样例太简单所以需要自己多列举。注意如果不相交,不要添加任何关系,因为两条线段无论以何种方式都不会互相干扰。
code
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define reset(a) memset((a),0,sizeof((a)))
using namespace std;
const int N=1000001;
struct edge{
int to,next;
}e[N];
int head[N],tot,HEAD[N];
int n,m,cnt,turn[N],belong[N],vis[N],a[N],b[N];
bool JG(int i,int j){
return ((a[i]<a[j]&&a[j]<b[i]&&b[j]>b[i])||(a[j]<a[i]&&a[i]<b[j]&&b[i]>b[j]));
}
void add(int x,int y){
e[++tot].to=y;e[tot].next=head[x];head[x]=tot;
e[++tot].to=x;e[tot].next=HEAD[y];HEAD[y]=tot;
}
void dfs1(int u){
int i;
vis[u]=1;
for(i=head[u];i;i=e[i].next)
if(!vis[e[i].to])
dfs1(e[i].to);
turn[++cnt]=u;
}
void dfs2(int u){
belong[u]=cnt;vis[u]=1;
for(int i=HEAD[u];i;i=e[i].next)
if(!vis[e[i].to])
dfs2(e[i].to);
}
void kosaraju(){
for(int i=1;i<=2*m;i++)
if(!vis[i])dfs1(i);
reset(vis);
cnt=0;
for(int i=2*m;i>=1;i--)
if(!vis[turn[i]])
cnt++,dfs2(turn[i]);
}
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>a[i]>>b[i];
if(a[i]>b[i])
swap(a[i],b[i]);
}
for(int i=1;i<=m;i++){
for(int j=i+1;j<=m;j++){
if(JG(i,j)){
// cout<<i<<" "<<j<<endl;
add(i,j+m);
add(j+m,i);
add(i+m,j);
add(j,i+m);
}
}
}
kosaraju();
for(int i=1;i<=m;i++)
if(belong[i]==belong[i+m]){
cout<<"the evil panda is lying again";return 0;
}
cout<<"panda is telling the truth...";
// for(int i=1;i<=m;i++){
// cout<<(belong[i]>belong[i+n])<<' ';
// }
return 0;
}