Simple 2-sat.
1 /*3678*/2#include <iostream>3#include <algorithm>4#include <cstdio>5#include <cstring>6#include <cstdlib>7 using namespacestd;8 9 #defineMAXN 1005Ten #defineMaxe 1000005 One Atypedefstruct { - intV, next; - } edge_t; the - intpre[maxn*2], low[maxn*2]; - intbn[maxn*2]; - intDfs_clock, block; + ints[maxn*2], top; - inthead[maxn*2], L; + edge_t E[maxe]; A intN, M, N2; at intA[maxe], B[maxe], c[maxe]; - CharOp[maxe]; - - voidAddedge (intUintv) { -E[L].V =v; -E[l].next =Head[u]; inHead[u] = l++; - } to + voidinit () { -Dfs_clock = block = top = L =0; thememset (PRE,0,sizeof(int)*n2); *Memset (BN,0,sizeof(int)*n2); $Memset (Head,-1,sizeof(int)*n2);Panax Notoginseng } - the voidDfsintu) { + intI, J, K, V; A thes[top++] =u; +Pre[u] = Low[u] = + +Dfs_clock; - for(I=head[u]; i!=-1; I=E[i].next) { $v =e[i].v; $ if(!Pre[v]) { - Dfs (v); -Low[u] =min (Low[u], low[v]); the}Else if(!Bn[v]) { -Low[u] =min (Low[u], pre[v]);Wuyi } the } - if(Low[u] = =Pre[u]) { Wu++Block; - Do { AboutBn[s[--top]] =Block; $} while(s[top]!=u); - } - } - A + the intMain () { - intI, J, K; $ intII, JJ; the Charcmd[8]; the BOOLFlag; the the #ifndef Online_judge -Freopen ("data.in","R", stdin); inFreopen ("Data.out","W", stdout); the #endif the About while(SCANF ("%d%d", &n,&m)! =EOF) { theN2 = n+N; the for(i=0; i<m; ++i) { thescanf"%d %d%d%s", &a[i], &b[i], &c[i], cmd); +Op[i] = cmd[0]; - } theFlag =true;Bayi for(k=0; flag&&k< +; ++k) { the init (); the for(i=0; i<m; ++i) { -II = a[i]+A[i]; -JJ = b[i]+B[i]; the if(Op[i] = ='A') { the // and the if(C[i] & (1<<k)) { theAddedge (II, ii+1); -Addedge (ii+1, jj+1); theAddedge (JJ, jj+1); theAddedge (jj+1, ii+1); the}Else {94Addedge (ii+1, JJ); theAddedge (jj+1, ii); the } the}Else if(Op[i] = ='O') {98 //or About if(C[i] & (1<<k)) { -Addedge (II, jj+1);101Addedge (JJ, ii+1);102}Else {103 Addedge (ii, JJ);104 Addedge (JJ, ii); theAddedge (ii+1, ii);106Addedge (jj+1, JJ);107 }108}Else if(Op[i] = ='X') {109 //XOR the if(C[i] & (1<<k)) {111Addedge (II, jj+1); theAddedge (ii+1, JJ);113Addedge (JJ, ii+1); theAddedge (jj+1, ii); the}Else { the Addedge (ii, JJ);117Addedge (ii+1, jj+1);118 Addedge (JJ, ii);119Addedge (jj+1, ii+1); - }121 }122 }123 for(i=0; i<n2; ++i)124 if(!Pre[i]) the DFS (i);126 for(i=0; flag&&i<n2; i+=2)127 if(Bn[i] = = bn[i+1]) -Flag =false;129 } the if(flag)131Puts"YES"); the Else133Puts"NO");134 }135 136 return 0;137}
"POJ" 3678 katu Puzzle