传送门:POJ 3207
Problem 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…
题意:
出一个圆上的 n 个点(0~n-1编号),然后在指定的 m 对点之间各连一条线(可以在圆内,也可以在圆外,可以是曲线),然后问你是否能使这些线都不相交。
题意有点难懂,看 oj 上的discuss上一个人的解释才看懂。
题解:
对于每一条边都有两个状态:连线在圆内和连线在圆外。再加上有m个边,可以看成是2-sat的模子。
那么题目的限制条件是线不相交,所以我们考虑圆上两线相交的条件。
假设有两条边i,j,第一条边两个端点为a1,b1,第二条边为a2,b2;
我们可以看成如果
a
1
<
a
2
且
b
1
<
b
2
且
b
1
>
a
2
a1<a2且b1<b2且b1>a2
a1<a2且b1<b2且b1>a2 的话两条线在圆的同一侧是一定相交的。
那么我们对于这两条边就可以这样建2-sat的图:
先将m条边按
a
a
a 有小到大排序
记
i
i
i 表示第 i 条边连在圆内,
i
′
i'
i′ 表示第 j 条边连在圆外。
那么建边就应该是:
i
i
i - >
j
′
j'
j′,
i
′
i'
i′ - >
j
j
j,
j
j
j - >
i
′
i'
i′,
j
′
j'
j′ - >
i
i
i
(
i
i
i - >
j
′
j'
j′表示边 i 连在圆内,边 j 就得连在圆外。其他的同理)。
建完图后,我们只要用2-sat跑一下判断可行性就行。
AC代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=1100;
int n,m,top;
bool mark[2*N];
int stack[2*N];
vector<int> g[2*N];
struct Node
{
int a,b;
}edges[N];
bool cmp(Node x,Node y)
{
return x.a<y.a;
}
void init()
{
memset(mark,false,sizeof(mark));
for(int i=0;i<2*N;i++)
{
g[i].clear();
}
}
void addedge(int x,int idx,int y,int idy)
{
x=2*x+idx;
y=2*y+idy;
g[x].push_back(y);
}
bool dfs(int x)
{
if(mark[x^1]) return false;
if(mark[x]) return true;
mark[x]=true;
stack[++top]=x;
int len=g[x].size();
for(int i=0;i<len;i++)
{
if(!dfs(g[x][i])) return false;
}
return true;
}
bool twosat()
{
for(int i=0;i<2*n;i+=2)
{
if(!mark[i]&&!mark[i+1])
{
top=0;
if(!dfs(i))
{
while(top>0)
mark[stack[top--]]=false;
if(!dfs(i+1)) return false;
}
}
}
return true;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
for(int i=0;i<m;i++)
{
scanf("%d%d",&edges[i].a,&edges[i].b);
if(edges[i].a>edges[i].b)
{
int tmp=edges[i].a;
edges[i].a=edges[i].b;
edges[i].b=tmp;
}
}
sort(edges,edges+m,cmp);
for(int i=0;i<m;i++)
{
for(int j=i+1;j<m;j++)
{
if(edges[j].b>edges[i].b&&edges[i].b>edges[j].a)
{
addedge(i,0,j,1);
addedge(i,1,j,0);
addedge(j,0,i,1);
addedge(j,1,i,0);
}
}
}
if(twosat()) printf("panda is telling the truth...\n");
else printf("the evil panda is lying again\n");
}
return 0;
}