POJ 3207 2-SAT
| Time Limit: 1000MS | Memory Limit: 131072K | |
| Total Submissions: 7124 | Accepted: 2648 |
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...
题意:很简单,不解释。
解题思路:首先根据起点,终点,判断线段是否相交,相交就建边,然后tarjin缩点判断。
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<string>
using namespace std;
struct node
{
int to,next;
}edge[9000000];
int head[3000],tol,low[3000],dfn[3000],indexx,Stack[300000],instack[3000],belong[3000],scc,top;
void add(int u,int v)
{
edge[tol].to=v;
edge[tol].next=head[u];
head[u]=tol++;
}
void tarjin(int u)
{
low[u]=dfn[u]=++indexx;
Stack[top++]=u;instack[u]=1;
int i,v;
for(int i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].to;
if(!dfn[v])
{
tarjin(v);
if(low[u]>low[v])low[u]=low[v];
}
else if(instack[v]&&low[u]>dfn[v])low[u]=dfn[v];
}
if(low[u]==dfn[u])
{
scc++;
do
{
v=Stack[--top];
instack[v]=0;
belong[v]=scc;
}while(u!=v);
}
}
struct Node
{
int st,ed;
}pp[10000];
int solve(int n)
{
memset(dfn,0,sizeof(dfn));
memset(instack,0,sizeof(instack));
memset(belong,0,sizeof(belong));
indexx=top=scc=0;
for(int i=0;i<2*n;i++)if(!dfn[i])tarjin(i);
//cout<<tol<<" "<<scc<<endl;
for(int i=0;i<n;i++)if(belong[2*i]==belong[2*i+1])return 0;
return 1;
}
bool judge(Node a,Node b)
{
if(a.st>b.st&&a.st<b.ed&&a.ed>b.ed)return 1;
if(b.st>a.st&&b.st<a.ed&&b.ed>a.ed)return 1;
return 0;
}
int main()
{
int i,j,k,m,n;
while(~scanf("%d%d",&n,&m))
{
memset(head,-1,sizeof(head));tol=0;
for(i=0;i<m;i++)
{
scanf("%d%d",&pp[i].st,&pp[i].ed);
if(pp[i].st>pp[i].ed)swap(pp[i].st,pp[i].ed);
}
for(i=0;i<m;i++)
for(j=i+1;j<m;j++)
if(judge(pp[i],pp[j]))
{
add(2*i,2*j+1);
add(2*i+1,2*j);
add(2*j,2*i+1);
add(2*j+1,2*i);
}
if(solve(m))puts("panda is telling the truth...");
else puts("the evil panda is lying again");
}
return 0;
}

浙公网安备 33010602011771号