poj 3678

      题目意思是,给出一系列的二元布尔运算,问所给变量是否存在可解的情况。这些布尔运算有与、或和异或运算。

       经典的2-sat问题。由于变量之间隐含着“冲突”关系,可以通过构造2-sat模型来求解。具体的建图规则为:

       若 a and b ==1 , !a->a , !b -> b

            a and b ==0 , a->!b , b->!a

            a or b ==1 , !a->b , !b->a

            a or b ==0 , a->!a , b->!b

            a xor b ==1 , a->!b,!b->a,!a->b,b->!a

            a xor b ==0 , a->b,b->a,!a->!b,!b->!a

 

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int M=1000007;
const int N=1020;
struct node
{
	int v;
	int next;
}edge[4*M];
int head1[2*N],head2[2*N],num;
int id[2*N],ord[2*N];
bool vis[2*N];
bool flag;
int n,m,cnt;

void init()
{
	for(int i=0;i<=2*n+1;i++)
	{
		head1[i]=-1;head2[i]=-1;
		id[i]=0;
	}
	num=0;
	flag=true;
}

void addege1(int u,int v)
{
	edge[num].v=v;
	edge[num].next=head1[u];
	head1[u]=num++;
}

void addege2(int u,int v)
{
	edge[num].v=v;
	edge[num].next=head2[u];
	head2[u]=num++;
}

void build()
{
	int  a,b,w;
	char s[10];
	for(int i=0;i<m;i++)
	{
		scanf("%d%d%d%s",&a,&b,&w,s);
		if(strcmp(s,"AND")==0)
		{
			if(w==1)
			{
				addege1(a*2+1,a*2);
				addege1(b*2+1,b*2);
				addege2(a*2,a*2+1);
				addege2(b*2,b*2+1);
			}
			else
			{
				addege1(a*2,b*2+1);
				addege1(b*2,a*2+1);
				addege2(b*2+1,a*2);
				addege2(a*2+1,b*2);
			}
		}
		else if(strcmp(s,"OR")==0)
		{
			if(w==1)
			{
				addege1(a*2+1,b*2);
				addege1(b*2+1,a*2);
				addege2(b*2,a*2+1);
				addege2(a*2,b*2+1);
			}
			else
			{
				addege1(a*2,a*2+1);
				addege1(b*2,b*2+1);
				addege2(a*2+1,a*2);
				addege2(b*2+1,b*2);
			}
		}
		else if(strcmp(s,"XOR")==0)
		{
			if(w==1)
			{
				addege1(a*2,b*2+1);
				addege1(b*2+1,a*2);
				addege1(a*2+1,b*2);
				addege1(b*2,a*2+1);
				addege2(b*2+1,a*2);
				addege2(a*2,b*2+1);
				addege2(b*2,a*2+1);
				addege2(a*2+1,b*2);
			}
			else
			{
				addege1(a*2,b*2);
				addege1(b*2,a*2);
				addege1(a*2+1,b*2+1);
				addege1(b*2+1,a*2+1);
				addege2(b*2,a*2);
				addege2(a*2,b*2);
				addege2(b*2+1,a*2+1);
				addege2(a*2+1,b*2+1);
			}
		}
	}
}

void dfs1(int u)
{
	vis[u]=1;
	for(int i=head1[u];i!=-1;i=edge[i].next)
	{
		int v=edge[i].v;
		if(!vis[v]) dfs1(v);
	}
	ord[cnt++]=u;
}

void dfs2(int u)
{
	vis[u]=1;
	id[u]=cnt;
	for(int i=head2[u];i!=-1;i=edge[i].next)
	{
		int v=edge[i].v;
		if(!vis[v]) dfs2(v);
	}
}

void kosaraju()
{
	cnt=0;
	fill(vis,vis+2*n,false);
	for(int i=0;i<2*n;i++)
	{
		if(!vis[i]) dfs1(i);
	}
	cnt=0;
	fill(vis,vis+2*n,false);
	for(int i=2*n-1;i>=0;i--)
	{
		if(!vis[ord[i]])
		{ 
			dfs2(ord[i]);
			++cnt;
		}
	}
	--cnt;
	for(int i=0;i<n;i++)
	{
		if(id[2*i]==id[2*i+1])
		{
			flag=false;return ;
		}
	}
}

int main()
{
	while(scanf("%d%d",&n,&m)==2)
	{
		init();
		build();
		kosaraju();
		if(flag) puts("YES");
		else puts("NO");
	}
	return 0;
}


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值