2-sat a simple question. Bit operation when writing to forget the parentheses wa ....
#include <cstdio>#include<cstring>#include<cmath>#include<queue>#include<vector>#include<stack>#include<algorithm>using namespacestd;Const intmaxn= -+Ten;intn,m;Chars[ +];inta,b,c;inta1=0, a2=1, b1=0, b2=1; Stack<int>S;vector<int>G[maxn];vector<int>FG[MAXN];intBELONG[MAXN];intFLAG[MAXN];intBlock;voidinit () { for(intI=0; i<maxn; i++) g[i].clear (); for(intI=0; i<maxn; i++) fg[i].clear (); memset (Belong,0,sizeofBelong); memset (Flag,0,sizeofflag); while(!S.empty ()) S.pop (); Block=0;}voidAddegde (intXinty) {g[x].push_back (y); Fg[y].push_back (x);}voidDFS1 (intNow ) {Flag[now]=1; for(intI=0; I<g[now].size (); i++) if(!Flag[g[now][i]] DFS1 (g[now][i]); S.push (now);}voidDFS2 (intNow ) {Belong[now]=Block; for(intI=0; I<fg[now].size (); i++) if(!Belong[fg[now][i]]) DFS2 (Fg[now][i]);}BOOLjudge () { for(intI=0; i<2*n; i++)if(!Flag[i]) dfs1 (i); while(!S.empty ()) { inttop=S.top (); S.pop (); if(!Belong[top]) {Block++; DFS2 (TOP); } } for(intI=0; i<n; i++) if(belong[2*i]==belong[2*i+1]) return 0; return 1;}intMain () { while(~SCANF ("%d%d",&n,&M)) {init (); for(intI=0; i<m; i++) {scanf ("%d%d%d%s",&a,&b,&c,s); if(strcmp (" and", s) = =0) { if((A1&B1)! =c) {Addegde (2*a,2*b+1); Addegde (2*b,2*a+1); } if((A1&B2)! =c) {Addegde (2*a,2*b); Addegde (2*b+1,2*a+1); } if((A2&B1)! =c) {Addegde (2*a+1,2*b+1); Addegde (2*b,2*a); } if((A2&B2)! =c) {Addegde (2*a+1,2*b); Addegde (2*b+1,2*a); } } Else if(strcmp ("OR", s) = =0) { if((A1|B1)! =c) {Addegde (2*a,2*b+1); Addegde (2*b,2*a+1); } if((A1|B2)! =c) {Addegde (2*a,2*b); Addegde (2*b+1,2*a+1); } if((A2|B1)! =c) {Addegde (2*a+1,2*b+1); Addegde (2*b,2*a); } if((A2|B2)! =c) {Addegde (2*a+1,2*b); Addegde (2*b+1,2*a); } } Else if(strcmp ("XOR", s) = =0) { if((A1^B1)! =c) {Addegde (2*a,2*b+1); Addegde (2*b,2*a+1); } if((A1^B2)! =c) {Addegde (2*a,2*b); Addegde (2*b+1,2*a+1); } if((A2^B1)! =c) {Addegde (2*a+1,2*b+1); Addegde (2*b,2*a); } if((A2^B2)! =c) {Addegde (2*a+1,2*b); Addegde (2*b+1,2*a); } } } if(judge ()) printf ("yes\n"); Elseprintf"no\n"); } return 0;}
POJ 3678 Katu Puzzle