Katu Puzzle
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12336 | Accepted: 4545 |
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.
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
X0 = 1, X1 = 1, X2 = 0, X3 = 1.
Source
POJ Founder Monthly Contest – 2008.07.27, Dagger
题意:
一个N个顶点和M条边的有向图,每个顶点能取0或1两个值.现在每条边被一个操作符(or,and,xor)以及一个值(0或1)标记了,表示a与b按操作符运算的结果是值(0或1).问你该有向图是否有可行解?
分析:
2-sat入门模板题
运算关系的转化:
根据什么能推出什么建边
and:
a and b = 0 : a=0 或 b=0 建边:2*a+1->2*b 2*b+1->2*a
a and b = 1 : a=1 且 b=1 建边: 2*a->2*a+1 2*b->2*b+1(只要a为0或b为0必然错误,所以我们只要添加上只要a=0则a就为1的矛盾命题即可)
or:
a or b = 0 : a=0且b=0 建边: 2*a+1->2*a 2*b+1->2*b(只要a为1或b为1必然错误)
a or b = 1 : a=1 或b=1 建边: 2*a->2*b+1 2*b->2*a+1
xor:
a xor b=0 : a=1且b=1 或 a=0且b=0 建边 :2*a->2*b 2*b->2*a 2*a+1->2*b+1 2*b+1->2*a+1.
a xor b=1 : a=1且b=0 或a=0且b=1 建边: 2*a+1->2*b 2*b->2*a+1 2*a->2*b+1 2*b+1->2*a
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=10000+10;
struct TwoSAT
{
int n;//原始图的节点数(未翻倍)
vector<int> G[maxn*2];//G[i]==j表示如果mark[i]=true,那么mark[j]也要=true
bool mark[maxn*2];//标记
int S[maxn*2],c;//S和c用来记录一次dfs遍历的所有节点编号
void init(int n)
{
this->n=n;
for(int i=0;i<2*n;i++) G[i].clear();
memset(mark,0,sizeof(mark));
}
//加入(x,xval)或(y,yval)条件
//xval=0表示假,yval=1表示真
void add_clause(int x,int xval,int y,int yval)
{
x=x*2+xval;
y=y*2+yval;
G[x].push_back(y);
// G[y^1].push_back(x);
}
//从x执行dfs遍历,途径的所有点都标记
//如果不能标记,那么返回false
bool dfs(int x)
{
if(mark[x^1]) return false;//这两句的位置不能调换
if(mark[x]) return true;
mark[x]=true;
S[c++]=x;
for(int i=0;i<G[x].size();i++)
if(!dfs(G[x][i])) return false;
return true;
}
//判断当前2-SAT问题是否有解
bool solve()
{
for(int i=0;i<2*n;i+=2)
if(!mark[i] && !mark[i+1])
{
c=0;
if(!dfs(i))
{
while(c>0) mark[S[--c]]=false;
if(!dfs(i+1)) return false;
}
}
return true;
}
}TS;
int main()
{
int n,m;
int a,b,c;
char op[10];
while(scanf("%d%d",&n,&m)!=-1)
{
TS.init(n);
for(int i=1;i<=m;i++){
scanf("%d %d %d %s",&a,&b,&c,&op);
if(op[0]=='A')
{
if(c==0)
{
TS.add_clause(a,1,b,0);
TS.add_clause(b,1,a,0);
}
else if(c==1)
{
TS.add_clause(a,0,a,1);
TS.add_clause(b,0,b,1);
}
}
else if(op[0]=='O')
{
if(c==0)
{
TS.add_clause(a,1,a,0);
TS.add_clause(b,1,b,0);
}
else if(c==1)
{
TS.add_clause(a,0,b,1);
TS.add_clause(b,0,a,1);
}
}
else if(op[0]=='X')
{
if(c==0)
{
TS.add_clause(a,0,b,0);
TS.add_clause(a,1,b,1);
TS.add_clause(b,0,a,0);
TS.add_clause(b,1,a,1);
}
else if(c==1)
{
TS.add_clause(a,0,b,1);
TS.add_clause(a,1,b,0);
TS.add_clause(b,0,a,1);
TS.add_clause(b,1,a,0);
}
}
}
if(TS.solve()) printf("YES\n");
else printf("NO\n");
}
return 0;
}