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:
AND 0 1
0 0 0
1 0 1
OR 0 1
0 0 1
1 1 1
XOR 0 1
0 0 1
1 1 0
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.
题意:有向图G(V,E),每个边E(a,b)由一个布尔算子op(AND,OR,XOR之一)和一个整数c(0≤c≤1)标记。如果可以找到每个顶点Vi a值Xi(0≤Xi≤1),那么对于由op和c标记的每个边e(a,b),以下题中公式成立
思路:2-sat问题,涉及三个对应关系,要分别判断。
建图:
op c Addedge
AND 0 i+n->j,j+n->i;
1 i->i+n,j->j+n;
OR 0 i+n->i,j+n->j;
1 i->j+n,j->i+n;
XOR 0 i+n->j+n.j->i,i->j,j+n->i+n;
1 i->j+n,j->i+n,i+n->j,j+n->i;
AC代码:
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<set>
#include<vector>
#include<queue>
#include<iostream>
using namespace std;
const int maxn = 1e6 + 100;
const int inf = 0x3f3f3f3f;
typedef long long ll;
struct node
{
int v, next;
}edge[maxn];
int head[maxn], dfn[maxn], low[maxn], instack[maxn], Stack[maxn], belong[maxn];
int n,m, tot, index, top, cnt;
void addedge(int u, int v)
{
edge[tot].v = v;
edge[tot].next = head[u];
head[u] = tot++;
return;
}
void tarjan(int u)
{
dfn[u] = low[u] = ++index;
instack[u] = 1;
Stack[top++] = u;
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].v;
if (!dfn[v])
{
tarjan(v);
low[u] = min(low[u], low[v]);
}
else if (instack[v])
{
low[u] = min(low[u], dfn[v]);
}
}
if (low[u] == dfn[u])
{
cnt++;
int v;
do
{
v = Stack[--top];
instack[v] = 0;
belong[v] = cnt;
} while (u != v);
}
}
int main()
{
while (scanf("%d%d", &n,&m) != EOF)
{
memset(head, -1, sizeof(head));
memset(dfn, 0, sizeof(dfn));
tot=index =0;
top = 0;
cnt = 0;
for (int i = 0; i < m; i++)
{
int x,y,z;
string s;
scanf("%d%d%d%d",&x,&y,&z);
cin>>s;
if(s=="AND")
{
if(z==1)
{
addedge(x,x+n);
addedge(y,y+n);
}
else
{
//addedge(x,y+n);
addedge(y+n,x);
//addedge(y,x+n);
addedge(x+n,y);
//addedge(x+n,y+n);
//addedge(x+n,y+n);
}
}
if(s=="OR")
{
if(z==1)
{
//addedge(x,y);
// addedge(y,x);
//addedge(x,y+n);
addedge(y,x+n);
addedge(x,y+n);
//addedge(y,x+n);
}
else
{
addedge(x+n,x);
addedge(y+n,y);
}
}
if(s=="XOR")
{
if(z==1)
{
addedge(x,y+n);
addedge(y+n,x);
addedge(x+n,y);
addedge(y,x+n);
}
else
{
addedge(x,y);
addedge(y,x);
addedge(x+n,y+n);
addedge(y+n,x+n);
}
}
}
for (int i = 0; i < n << 1; i++)
{
if (!dfn[i])
{
tarjan(i);
}
}
int flag = 0;
for (int i =0;i<n; i++)
{
if (belong[i] == belong[i+n])
{
flag =1;
}
}
if (flag)
{
printf("NO\n");
}
else
{
printf("YES\n");
}
}
return 0;
}