Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 9609 | Accepted: 3525 |
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
题目大意:
一个圆由n个点组成,有m条边需要连接,问是否能在连接过程中使得每条线都不相交。每条线有两种选择,要么从圆外连接,要么从圆内连接。
分析一组样例:
1 5
2 6
3 7
这就是一组必然要有相交的方案。
思路:
1、首先分析:一条线要么从圆内连接,要么从圆外连接,那么我们将每条边看成一个帮派,内部连接表示一个点,外部连接也表示一个点,那么一条边可以分成两个点:
(i,i+m)。,i可以表示第i条边在圆外连接,i+m可以表示第i条边在圆内连接。
2、然后分析矛盾边:
对于这样一种情况,明显两条边需要一个在圆外连接,一个在园内连接才行,那么这样两条曲线可以构成矛盾边:
(i,j)【这样两条边不能同时在圆外连接】------------>对应建边(i,j+m),(j,i+m);
(i+m,j+m)【这样两条边不能同时在圆内连接】------------->对应建边(j+m,i),(i+m,j);
3、对应将每条矛盾边加入图中,2-sat判定,跑一遍Tarjan,对应缩点染色,如果对应每一个i和i+m都不是同一个颜色,那么就输出panda is telling the truth...。否则说明有矛盾(一定要有相交的地方),输出另一个语句。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
struct node
{
int from;
int to;
int next;
int w;
}e[1000000];
int n,m,cont,cnt,sig,tt;
int head[10000];
int bian[5050][2];
int stack[10000];
int vis[10000];
int dfn[10000];
int low[10000];
int color[10000];
void add(int from,int to)
{
e[cont].from=from;
e[cont].to=to;
e[cont].next=head[from];
head[from]=cont++;
}
void getmap()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
int x=bian[i][0];
int y=bian[i][1];
int x2=bian[j][0];
int y2=bian[j][1];
if(x2>x&&y2>y&&y>x2)
{
add(i,j+m);
add(j+m,i);
add(i+m,j);
add(j,i+m);
}
}
}
}
void Dfs(int u)
{
vis[u]=1;
stack[++tt]=u;
dfn[u]=low[u]=cnt++;
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
if(vis[v]==0)
{
Dfs(v);
}
low[u]=min(low[u],low[v]);
}
if(dfn[u]==low[u])
{
sig++;
do
{
color[stack[tt]]=sig;
vis[stack[tt]]=-1;
}
while(stack[tt--]!=u);
}
}
void Tarjan()
{
tt=-1;sig=0;cnt=1;
memset(stack,0,sizeof(stack));
memset(color,0,sizeof(color));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(vis,0,sizeof(vis));
for(int i=0;i<m*2;i++)
{
if(vis[i]==0)
{
Dfs(i);
}
}
int flag=0;
for(int i=0;i<m;i++)
{
if(color[i]==color[i+m])
{
flag=1;
printf("the evil panda is lying again\n");
break;
}
}
if(flag==0)
{
printf("panda is telling the truth...\n");
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
cont=0;
memset(head,-1,sizeof(head));
for(int i=0;i<m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
if(x>y)swap(x,y);
bian[i][0]=x;
bian[i][1]=y;
}
getmap();
Tarjan();
}
}