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...
题意:
圆上一共有n个点,想要连m条边(左右端点分别为x,y),边可以在园内连,也可以
在圆外连,求能否使得所有边不相交。
思路:
以每条线段为点,那么对于两点之间的一条位于圆内部的曲线 B,肯定有一条对应的位于圆外部的曲线 B‘,显然这两个点满足 2-SAT 问题模型中的 “B 与 非B” 的关系,然后对于每对两条同在圆内部(或者外部)的曲线来说,通过他们的起点,终点坐标就可以判断2者是否矛盾了,从而依赖这个关系建图,剩下的就是塞模板了。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 2000005;
struct node{
int v;
int next;
}edge[N];
int id,head[N];
int LOW[N],DFN[N],index;
int vis[N],fa[N],type;
int st[N],top;
int x[N],y[N];
int n,m;
void init(){
id=index=top=type=0;
memset(LOW,0,sizeof(LOW));
memset(DFN,0,sizeof(DFN));
memset(vis,0,sizeof(vis));
memset(head,-1,sizeof(head));
}
void add(int u,int v){
edge[id].v=v;
edge[id].next=head[u];
head[u]=id++;
}
int judge(int i,int j){
if(x[i]<=x[j]&&y[i]>=x[j]&&y[i]<=y[j]) return 1;
if(x[i]>=x[j]&&x[i]<=y[j]&&y[i]>=y[j]) return 1;
return 0;
}
void build(){
for(int i=1;i<=m;i++){
scanf("%d%d",&x[i],&y[i]);
x[i]++;
y[i]++;
if(x[i]>y[i]) swap(x[i],y[i]);
}
for(int i=1;i<=m;i++){
for(int j=i+1;j<=m;j++)
if(judge(i,j)){
add(i,j+m);
add(j,i+m);
add(j+m,i);
add(i+m,j);
}
}
}
void tarjan(int u){
DFN[u]=LOW[u]=++index;
vis[u]=1;
st[top++]=u;
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].v;
if(!DFN[v]){
tarjan(v);
LOW[u]=min(LOW[u],LOW[v]);
}
else if(vis[v]) LOW[u]=min(LOW[u],DFN[v]);
}
if(LOW[u]==DFN[u]){
int v=u;
fa[u]=++type;
do{
v=st[--top];
vis[v]=0;
fa[v]=type;
}while(u!=v);
}
}
int check(){
for(int i=1;i<=m;i++)
if(fa[i]==fa[m+i]) return 0;
return 1;
}
void solve(){
for(int i=1;i<=m;i++)
if(!DFN[i]) tarjan(i);
if(check()) printf("panda is telling the truth...\n");
else printf("the evil panda is lying again\n");
}
int main(){
scanf("%d%d",&n,&m);
init();
build();
solve();
return 0;
}