2-sat, split into six points.
#include <cstdio>#include<cstring>#include<cmath>#include<stack>#include<queue>#include<algorithm>using namespacestd;Const intmaxn=60000+Ten;intT,n,m;stack<int>S;vector<int>G[maxn];vector<int>FG[MAXN];intBELONG[MAXN];intFLAG[MAXN];intBlock;intX[MAXN];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;}voidAddedge (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<6*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<3*n; i++) if(belong[i]==belong[i+3*N])return 0; return 1;}voidRead () {intA,b,c; for(intI=0; i<n;i++) {Addedge (3*i+0,3*i+1+3*N); Addedge (3*i+0,3*i+2+3*N); Addedge (3*i+1,3*i+0+3*N); Addedge (3*i+1,3*i+2+3*N); Addedge (3*i+2,3*i+0+3*N); Addedge (3*i+2,3*i+1+3*N); } for(intI=0; i<n;i++) {scanf ("%d", &x[i]); x[i]--; if(x[i]==0) {Addedge (3*i+1+3*n,3*i+0); Addedge (3*i+0+3*n,3*i+1); } Else if(x[i]==1) {Addedge (3*i+1+3*n,3*i+2); Addedge (3*i+2+3*n,3*i+1); } Else if(x[i]==2) {Addedge (3*i+0+3*n,3*i+2); Addedge (3*i+2+3*n,3*i+0); } } for(intI=1; i<=m;i++) {scanf ("%d%d%d",&a,&b,&b); A--, b--; if(c==0) {Addedge (3*a+0,3*b+0); Addedge (3*a+1,3*b+1); Addedge (3*a+2,3*b+2); Addedge (3*b+0,3*a+0); Addedge (3*b+1,3*a+1); Addedge (3*b+2,3*a+2); } Else if(c==1) {Addedge (3*a+0,3*b+0+3*N); Addedge (3*a+1,3*b+1+3*N); Addedge (3*a+2,3*b+2+3*N); Addedge (3*b+0,3*a+0+3*N); Addedge (3*b+1,3*a+1+3*N); Addedge (3*b+2,3*a+2+3*N); } }}intMain () {scanf ("%d",&T); for(intCase=1; case<=t; case++) {scanf ("%d%d",&n,&M); Init (); Read (); printf ("Case #%d:", case); if(judge ()) printf ("yes\n"); Elseprintf"no\n"); } return 0;}
HDU 4115 Eliminate the Conflict