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:
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. 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 |
题目:http://poj.org/problem?id=3678
题意:给你n个布尔型变量,给你一个关系表,标志n个变量之间的关系,问这样的关系是否合法。。。。
分析:好吧,由于是在2-sat分类里面看到的题,所有直接想到2-sat,只要分清3个关系式,就可以建图了,比如and并且为1,则两个变量同时为1,and为0的话,只要保证一个是0就行。。。
代码:
#include<cstdio>
#include<iostream>
using namespace std;
const int mm=4444444;
const int mn=2222;
int ver[mm],next[mm];
int head[mn],dfn[mn],low[mn],q[mn],id[mn];
int i,j,k,n,m,idx,top,cnt,edge;
void add(int u,int v)
{
ver[edge]=v,next[edge]=head[u],head[u]=edge++;
}
void dfs(int u)
{
dfn[u]=low[u]=++idx;
q[top++]=u;
for(int i=head[u],v;i>=0;i=next[i])
if(!dfn[v=ver[i]])
dfs(v),low[u]=min(low[u],low[v]);
else if(!id[v])low[u]=min(low[u],dfn[v]);
if(dfn[u]==low[u])
{
id[u]=++cnt;
while(q[--top]!=u)id[q[top]]=cnt;
}
}
void Tarjan()
{
for(idx=top=cnt=i=0;i<n+n;++i)dfn[i]=id[i]=0;
for(i=0;i<n+n;++i)
if(!dfn[i])dfs(i);
}
bool ok()
{
for(i=0;i<n+n;i+=2)
if(id[i]==id[i^1])return 0;
return 1;
}
char op[22];
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(edge=i=0;i<n+n;++i)head[i]=-1;
while(m--)
{
scanf("%d%d%d%s",&i,&j,&k,op);
if(op[0]=='A')
{
if(k)add(i<<1,i<<1|1),add(j<<1,j<<1|1);
else add(i<<1|1,j<<1),add(j<<1|1,i<<1);
}
if(op[0]=='O')
{
if(k)add(i<<1,j<<1|1),add(j<<1,i<<1|1);
else add(i<<1|1,i<<1),add(j<<1|1,j<<1);
}
if(op[0]=='X')
{
if(k)add(i<<1|1,j<<1),add(j<<1|1,i<<1),add(i<<1,j<<1|1),add(j<<1,i<<1|1);
else add(i<<1,j<<1),add(j<<1,i<<1),add(i<<1|1,j<<1|1),add(j<<1|1,i<<1|1);
}
}
Tarjan();
puts(ok()?"YES":"NO");
}
return 0;
}