POJ3207+tarjan+2-sat

时间:2023-03-08 17:51:47
POJ3207+tarjan+2-sat
 /*
2-sat
题意:给定一个圆,圆上一些点。两点一线。现给出一些线,这些线可以在圆内连起来,也可以在圆外。
问有没有可能所有的线画完 且 不出现相交。
思路:把线画在圆内或圆外 看成一个组合。其它的则建边。
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<vector>
using namespace std; typedef long long int64;
//typedef __int64 int64;
typedef pair<int64,int64> PII;
#define MP(a,b) make_pair((a),(b))
const int maxn = ;
const int maxm = ;
const int inf = 0x7fffffff;
const double pi=acos(-1.0);
const double eps = 1e-; struct Edge{
int u,v,next;
}edge[ maxm*+ ];
int cnt,head[ maxn ];
int vis[ maxn ];
int dfn[ maxn ],low[ maxn ];
int belong[ maxn ],Cnt,id;
stack<int>s;
struct EDGE{
int l,r;
}E[ maxn ]; void init(){
id = Cnt = ;
cnt = ;
while( !s.empty() )
s.pop();
memset( head,-,sizeof( head ) );
memset( vis,-,sizeof( vis ) );
memset( dfn,-,sizeof( dfn ) );
memset( low,-,sizeof( low ) );
} void addedge( int a,int b ){
edge[ cnt ].u = a;
edge[ cnt ].v = b;
edge[ cnt ].next = head[ a ];
head[ a ] = cnt++;
} bool ok( int L,int R ){
int x1 = E[L].l;
int y1 = E[L].r;
int x2 = E[R].l;
int y2 = E[R].r;
if( x2>x1&&x2<y1 ){
if( y2>=y1 ) return true;
if( y2<=x1 ) return true;
}
if( y2>x1&&y2<y1 ){
if( x2>=y1 ) return true;
if( x2<=x1 ) return true;
}
return false;
} void tarjan( int cur ){
dfn[ cur ] = low[ cur ] = id++;
vis[ cur ] = ;
s.push( cur );
for( int i=head[ cur ];i!=-;i=edge[i].next ){
int nxt = edge[ i ].v;
if( dfn[ nxt ]==- ){
tarjan( nxt );
low[ cur ] = min( low[ cur ],low[ nxt ] );
}
else if( vis[ nxt ]== ){
low[ cur ] = min( low[ cur ],dfn[ nxt ] );
}
}
if( dfn[ cur ]==low[ cur ] ){
Cnt ++;
while( ){
int tmp = s.top();
s.pop();
vis[ tmp ] = ;
belong[ tmp ] = Cnt;
if( tmp==cur ) break;
}
}
} int main(){
int n,m;
while( scanf("%d%d",&n,&m)== ){
init();
int a,b;
for( int i=;i<=m;i++ ){
scanf("%d%d",&a,&b);
a++,b++;
E[ i ].l = min( a,b );
E[ i ].r = max( a,b );
}
for( int i=;i<=m;i++ ){
for( int j=i+;j<=m;j++ ){
if( ok( i,j )==true ){
addedge( i,j+m );
addedge( j+m,i );
addedge( i+m,j );
addedge( j,i+m );
}
}
}//build mat
for( int i=;i<=*m;i++ ){
if( dfn[i]==- ){
tarjan( i );
}
}
//
bool f = true;
for( int i=;i<=m;i++ ){
if( belong[i]==belong[i+m] ){
f = false;
break;
}
}
if( f==false ) printf("the evil panda is lying again");
else printf("panda is telling the truth...");
printf("\n");
}
return ;
}