POJ 3207 Ikki's Story IV - Panda's Trick(2-SAT入门)

讲解见:http://hi.baidu.com/chinaeli/blog/item/dd00463cdb40eecf9f3d62c7.html

http://blog.163.com/lfw2565295@126/blog/static/12200516201102012251212/

 

代码:

#include<stdio.h>
#include
<math.h>
#include
<iostream>
using namespace std;
const int MAXN=1005;
const int MAXM=1000005;
struct Node
{
int l,r;
}e[MAXN];
struct Node1
{
int from,to,next;
}edge1[MAXM],edge2[MAXM];
int visit1[MAXN],visit2[MAXN],head1[MAXN],head2[MAXN],Belong[MAXN],T[MAXN];
int tol1,tol2,Bcnt,Tcnt;
void add(int a,int b)
{
edge1[tol1].from
=a;edge1[tol1].to=b;edge1[tol1].next=head1[a];head1[a]=tol1++;
edge2[tol2].from
=b;edge2[tol2].to=a;edge2[tol2].next=head2[b];head2[b]=tol2++;
}
void dfs1(int x)
{
int j;
visit1[x]
=1;
for(j=head1[x];j!=-1;j=edge1[j].next)
if(visit1[edge1[j].to]==0) dfs1(edge1[j].to);
T[Tcnt
++]=x;
}
void dfs2(int x)
{
int j;
visit2[x]
=1;
Belong[x]
=Bcnt;
for(j=head2[x];j!=-1;j=edge2[j].next)
if(visit2[edge2[j].to]==0) dfs2(edge2[j].to);
}
double dist(int x1,int y1,int x2,int y2)
{
return sqrt((double)(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
int main()
{
int i,j,n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0;i<m;i++)
{
scanf(
"%d%d",&e[i].l,&e[i].r);
/*if(e[i].l>e[i].r)
{
int tmp=e[i].l;
e[i].l=e[i].r;
e[i].r=e[i].l;
}
*/ //这个可以不加上去
}
for(i=0;i<2*m;i++)
{
head1[i]
=-1;
head2[i]
=-1;
visit1[i]
=0;
visit2[i]
=0;
}
tol1
=tol2=Bcnt=Tcnt=0;
for(i=0;i<m;i++)
for(j=i+1;j<m;j++)
{
if((e[i].l<e[j].l&&e[j].l<e[i].r&&e[i].r<e[j].r)||(e[j].l<e[i].l&&e[i].l<e[j].r&&e[j].r<e[i].r))
{
add(
2*i,2*j+1);
add(
2*j+1,2*i);
add(
2*j,2*i+1);
add(
2*i+1,2*j);
}
}
for(i=0;i<2*m;i++)
if(visit1[i]==0) dfs1(i);
for(i=Tcnt-1;i>=0;i--)
{
if(visit2[T[i]]==0)
{
dfs2(T[i]);
Bcnt
++;
}
}
for(i=0;i<=2*m-2;i+=2)
{
if(Belong[i]==Belong[i+1])break;
}
if(i>2*m-2) printf("panda is telling the truth...\n");
else
printf(
"the evil panda is lying again\n");

}
return 0;
}
    原文作者:kuangbin
    原文地址: https://www.cnblogs.com/kuangbin/archive/2011/08/23/2150098.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞