poj 3207

2sat

建好图就没问题。

代码:

#include<iostream>
#include<fstream>

using namespace std;

struct e{
	int data;
	e *next;
}edge[1001];
int n,m;

int a[501],b[501];

int cmp(const void *a,const void *b){
	return *(int *)a-*(int *)b;
}

int v[1001],low[1001],dfn[1001],stack[1001],scc[1001],tot,index,top;

void tarjan(int s){
	int i,j,k;
	dfn[s]=low[s]=++index;
	stack[++top]=s;
	v[s]=1;
	e *p=edge[s].next;
	while(p){
		if(dfn[p->data]==0)
		{
			tarjan(p->data);
			low[s]=min(low[s],low[p->data]);
		}
		else
			if(v[p->data])
			{
				low[s]=min(low[s],dfn[p->data]);
			}
		p=p->next;
	}
	if(low[s]==dfn[s]){
		++tot;
		
		do{
			i=stack[top--];
			scc[i]=tot;
			
			v[i]=0;
		}while(i!=s);
	}
}



void solve(){
	int i,j,k;
	index=0;tot=0;top=0;
	memset(v,0,sizeof(v));
	memset(dfn,0,sizeof(dfn));
	for(i=1;i<=2*m;i++)
		if(dfn[i]==0)
			tarjan(i);
}


int ok(int s,int t){
	int c[4],i,j,k;
	c[0]=a[s];
	c[1]=a[t];
	c[2]=b[s];
	c[3]=b[t];
	qsort(c,4,sizeof(int),cmp);
	if(c[0]==a[s]||c[0]==b[s])
		j=0;
	else j=1;
	for(i=1;i<4;i++)
	{
		if(c[i]==a[s]||c[i]==b[s])
			k=0;
		else
			k=1;
		if(k==j) return 0;
		j=k;
	}
	return 1;
}

void read(){
//	ifstream cin("in.txt");
	int i,j,k;
	cin>>n>>m;
	for(i=1;i<=m;i++)
	{
		cin>>a[i]>>b[i];
	}
	for(i=1;i<=m;i++)
		for(j=i+1;j<=m;j++)
		{
			if(ok(i,j))
			{
				e *p=new e;
				p->data=j+m;
				p->next=edge[i].next;
				edge[i].next=p;
				e *q=new e;
				q->data=j;
				q->next=edge[i+m].next;
				edge[i+m].next=q;
				e *p1=new e;
				p1->data=i+m;
				p1->next=edge[j].next;
				edge[j].next=p1;
				e *q1=new e;
				q1->data=i;
				q1->next=edge[j+m].next;
				edge[j+m].next=q1;
			}
		}
	solve();
	for(i=1;i<=m;i++)
		if(scc[i]==scc[i+m])
		{
			
			break;
		}
	if(i<=m) cout<<"the evil panda is lying again"<<endl;
	else
		cout<<"panda is telling the truth..."<<endl;



}

int main(){
	read();
	return 0;
}

posted on 2011-03-19 10:27  宇宙吾心  阅读(408)  评论(0)    收藏  举报

导航