HDU 4115 Eliminate the Conflict
2-SAT,拆成六个点。
#include<cstdio> #include<cstring> #include<cmath> #include<stack> #include<queue> #include<algorithm> using namespace std; const int maxn=60000+10; int T,N,M; stack<int>S; vector<int>G[maxn]; vector<int>FG[maxn]; int Belong[maxn]; int flag[maxn]; int Block; int X[maxn]; void init() { for(int i=0; i<maxn; i++) G[i].clear(); for(int i=0; i<maxn; i++) FG[i].clear(); memset(Belong,0,sizeof Belong); memset(flag,0,sizeof flag); while(!S.empty()) S.pop(); Block=0; } void addEdge(int x,int y) { G[x].push_back(y); FG[y].push_back(x); } void dfs1(int now) { flag[now]=1; for(int i=0; i<G[now].size(); i++) if(!flag[G[now][i]]) dfs1(G[now][i]); S.push(now); } void dfs2(int now) { Belong[now]=Block; for(int i=0; i<FG[now].size(); i++) if(!Belong[FG[now][i]]) dfs2(FG[now][i]); } bool judge() { for(int i=0; i<6*N; i++) if(!flag[i]) dfs1(i); while(!S.empty()) { int Top=S.top(); S.pop(); if(!Belong[Top]) { Block++; dfs2(Top); } } for(int i=0; i<3*N; i++) if(Belong[i]==Belong[i+3*N]) return 0; return 1; } void read() { int A,B,C; for(int i=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(int i=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(int i=1;i<=M;i++) { scanf("%d%d%d",&A,&B,&C); 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); } } } int main() { scanf("%d",&T); for(int Case=1;Case<=T;Case++) { scanf("%d%d",&N,&M); init(); read(); printf("Case #%d: ",Case); if(judge()) printf("yes\n"); else printf("no\n"); } return 0; }