POJ3678——Katu Puzzle

Description

Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a boolean operator op (one of AND, OR, XOR) and an integer c (0 ≤ c ≤ 1). One Katu is solvable if one can find each vertex Vi a value Xi (0 ≤ Xi ≤ 1) such that for each edge e(a, b) labeled by op and c, the following formula holds:

 Xa op Xb = c

The calculating rules are:

AND01
000
101
OR01
001
111
XOR01
001
110

Given a Katu Puzzle, your task is to determine whether it is solvable.

Input

The first line contains two integers N (1 ≤ N ≤ 1000) and M,(0 ≤ M ≤ 1,000,000) indicating the number of vertices and edges.
The following M lines contain three integers a (0 ≤ a < N), b(0 ≤ b < N), c and an operator op each, describing the edges.

Output

Output a line containing "YES" or "NO".

Sample Input

4 4
0 1 1 AND
1 2 1 OR
3 2 0 AND
3 0 0 XOR

Sample Output

YES

Hint

X 0 = 1, X 1 = 1, X 2 = 0, X 3 = 1.

Source


题目很简单,但是我建图还是建错了,后来看了别人的,才知道当a&b==1和a|b==0时,必须建立  (~a -> a)这样的边,比如a&b==1,a和b显然是取 1的,那么如果一旦a取了0,根据有边(~a->a), 马上推出矛盾


#include<stdio.h>
#include<string.h>

const int N=2010;
const int maxm=9000010;
struct node
{
	int to;
	int next;
}edge[maxm];

char str[10];
int head[N];
int tot,index,sccnum,top;
int DFN[N],low[N];
int Stack[N];
bool instack[N];
int block[N];

int min(int a,int b)
{
	return a<b?a:b;
}

void addedge(int from,int to)
{
	edge[tot].to=to;
	edge[tot].next=head[from];
	head[from]=tot++;
}

void tarjan(int u)
{
	DFN[u]=low[u]=++index;
	Stack[++top]=u;
	instack[u]=1;
	for(int i=head[u];i!=-1;i=edge[i].next)
	{
		int v=edge[i].to;
		if(!DFN[v])
		{
			tarjan(v);
			low[u]=min(low[v],low[u]);
		}
		else if(instack[v])
			low[u]=min(low[u],DFN[v]);
	}
	if(DFN[u]==low[u])
	{
		sccnum++;
		int v;
		do
		{
			v=Stack[top--];
			instack[v]=0;
			block[v]=sccnum;
		}while(v!=u);
	}
}

void init()
{
	memset(DFN,0,sizeof(DFN));
	memset(low,0,sizeof(low));
	memset(head,-1,sizeof(head));
	memset(instack,0,sizeof(instack));
	tot=0;
	index=0;
	sccnum=0;
}

int main()
{
	int n,m;
	while(~scanf("%d%d",&n,&m))
	{
		init();
		int a,b,c;
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d%d%s",&a,&b,&c,str);
			if(!strcmp(str,"AND") && c)
			{
				//and运算
				addedge(a+n,b+n);
				addedge(b+n,a+n);
				addedge(a,a+n);
				addedge(b,b+n);
			}
			else if(!strcmp(str,"AND") && !c)
			{
				addedge(a+n,b);
				addedge(b+n,a);
				
			}
			else if(!strcmp(str,"OR") && c)
			{
				addedge(a,b+n);
				addedge(b,a+n);
			}
			else if(!strcmp(str,"OR") && !c)
			{
				addedge(a,b);
				addedge(b,a);
				addedge(a+n,a);
				addedge(b+n,b);
			}
			else if(!strcmp(str,"XOR") && c)
			{
				addedge(a,b+n);
				addedge(a+n,b);
				addedge(b,a+n);
				addedge(b+n,a);
			}
			else
			{
				addedge(a,b);
				addedge(b,a);
				addedge(a+n,b+n);
				addedge(b+n,a+n);
			}
		}
		for(int i=0;i<n+n;i++)
			if(!DFN[i])
				tarjan(i);
		bool flag=false;
		for(int i=0;i<n;i++)
			if(block[i]==block[i+n])
			{
				flag=true;
				printf("NO\n");
				break;
			}
		if(!flag)
			printf("YES\n");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值