With N variables X- 1 the X1 ~ X- N XN, possible values of each variable is 0 or 1.
Given the M equations, each equation of the form X- A O P X- B = C XaopXb = C, where a, b are variable numbers, c is the number 0 or 1, op is and, or, xor Bitwise of three One.
Ask whether there is a legal assignment to each variable, so that all equations are true.
Input Format
The first line contains two integers N and M.
Next M rows, each row containing three integers ABC, and a bit operation (AND, OR, XOR one).
Output Format
The output, if present, output "YES", otherwise a "NO"
data range
1≤N≤10001≤N≤1000,
1≤M≤1061≤M≤106
Sample input:
4 4
0 1 1 AND 1 2 1 OR 3 2 0 AND 3 0 0 XOR
Sample output:
YES
Ideas: The meaning of the questions, the problem is a 2-SAT problem, the conversion operator, can be determined
For A [x], may be 'implemented <, x, NOT A [x], can even edge <x, x through the communicating edge X>' implemented> for NOT (A [x] AND A [y]) You need to connect two edges <x, y '> and <y, x'> be achieved, for A [x] OR A [y] need to connect two edges <x ', y> and <y', x> to achieve
So for and, or, xor three operations are:
and operations:
a and b = 0: If there is a = 1, then there must satisfy b = 0; if b = 1, then there must satisfy a = 0, namely: <a, 1, b, 0>, <b, 1, a, 0>
A = B and when. 1: a = 1 and b = 1, namely: <A, 0, A,. 1>, <B, 0, B,. 1>
or operation:
a or b = 0 when: a = 0 and b = 0, ie: <A,. 1, A, 0>, <B,. 1, B, 0>
A or B =. 1: If there is a = 0, then there must satisfies b = 1; if b = 0, then there must satisfy a = 0, namely: <A, 0, B,. 1>, <B, 0, A,. 1>
XOR operation:
a xor b = 0, the following four cases:
if a = 0, then there must satisfy b = 0, ie: <a, 0, b, 0>
if b = 0, then there must satisfy a = 0, namely: <b, 0, a, 0 >
if a = 1, then there must satisfy b = 1, namely: <a, 1, b, 1>
if b = 1, then there must satisfy a = 1, namely: <b, 1 , a,. 1>
a = B XOR. 1, the following four cases:
if a = 0, then there must satisfy b = 1, namely: <a, 0, b, 1>
if b = 0, then a must be satisfied = 1, namely: <b, 0, a, 1>
if a = 1, then there must satisfy b = 0, ie: <a, 1, b, 0>
if b = 1, then there must satisfy a = 0, i.e., : <b, 1, a, 0>
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring>usingnamespace std; #define debug(x) cout << "fuck bug " << x << "\n"; #define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) constint maxn = 4e5 + 7; typedef longlong ll;int n,m;struct edge {int to,nxt; }e[maxn];int head[maxn],tot; void add(int u , and [int v){++tot].to = v; e[tot].nxt = head[u]; head[u] = tot; } int dfn[maxn],low[maxn],num,inStack[maxn]; int stack[maxn],top,cnt,C[maxn]; void tarjan(int x) { dfn[x] = low[x] = ++num; stack[++top] = x; inStack[x] = true; for (int i = head[x]; i; i = e[i].nxt) { int y = e[i].to; if (dfn[y] == 0) { tarjan(y); low[x] = min(low[x], low[y]); } else if (inStack[y]) { low[x] = min(low[x], low[y]); } } if (low[x] == dfn[x]) { cnt++; int z; do { z = stack[top--]; inStack[z] = false; C[z] = cnt; } while (z != x); } } int main(int argc, char const *argv[]) { //cin >> n >> m; scanf("%d %d",&n,&m); for(int i = 1;i <= m ; i ++){ int a , b , c ; char str[10]; //cin >> a >> b >> c >> str; scanf("%d %d %d %s",&a,&b,&c,str); if(str[0] == 'A'){ if(c == 1){ add(a,a+n); add(b,b+n); }else{ add(a+n,b); add(b+n,a); } }else if(str[0] == 'O'){ if(c == 1){ add(a,b+n); add(b,a+n); }else{ add(a+n,a); add(b+n,b); } }else if(str[0] == 'X'){ if(c == 1){ add(a,b+n); add(b,a+n); add(a+n,b); add(b+n,a); }else{ add(a,b); add(b,a); add(a+n,b+n); add(b+n,a+n); } } } for(int i = 0;i < n + n; i++){ if(!dfn[i]) tarjan(i); } bool flag=1; for(int i = 0;i < n ;i ++){ if(C[i] == C[i + n]) { flag = 0; break; } } if(flag) puts("YES"); else puts("NO"); return 0; }