2-sat题
// File Name: 10319.cpp // Author: Zlbing // Created Time: 2013/4/11 18:02:23 #include<iostream> #include<string> #include<algorithm> #include<cstdlib> #include<cstdio> #include<set> #include<map> #include<vector> #include<cstring> #include<stack> #include<cmath> #include<queue> using namespace std; #define CL(x,v); memset(x,v,sizeof(x)); #define INF 0x3f3f3f3f #define LL long long #define REP(i,r,n) for(int i=r;i<=n;i++) #define RREP(i,n,r) for(int i=n;i>=r;i--) #define MAXN 100 struct TwoSAT{ int n; vector<int>G[MAXN*2]; bool mark[MAXN*2]; stack<int>S; bool dfs(int x) { if(mark[x^1])return false; if(mark[x])return true; mark[x]=true; S.push(x); for(int i=0;i<G[x].size();i++) { int v=G[x][i]; if(!dfs(v))return false; } return true; } void init(int _n) { n=_n; for(int i=0;i<2*n;i++) G[i].clear(); memset(mark,0,sizeof(mark)); } void add_clause(int x,int y) { G[x].push_back(y); } bool solve() { for(int i=0;i<2*n;i=i+2) { if(!mark[i]&&!mark[i+1]){ while(!S.empty()) { S.pop(); } if(!dfs(i)) { while(!S.empty()) { mark[S.top()]=false; S.pop(); } if(!dfs(i+1))return false; } } } // for(int i=0;i<2*n;i++) // if(mark[i])printf("%d ",T[i/2][i%2]); // printf("\n"); return true; } }; TwoSAT solver; int main() { int T; scanf("%d",&T); while(T--) { int S,A,m; scanf("%d%d%d",&S,&A,&m); solver.init(S+A); int a1,s1,a2,s2; REP(i,1,m) { scanf("%d%d%d%d",&s1,&a1,&s2,&a2); s1--,a1--,s2--,a2--; a1*=2;s1=2*A+2*s1;a2*=2;s2=2*(A+s2); if(s1==s2&&a1==a2)continue; if(a2<a1) { s1=s1^1; s2=s2^1; } if(s2<s1) { a1^=1; a2^=1; } if(a1==a2) { solver.add_clause(a1^1,a2); continue; } if(s1==s2) { solver.add_clause(s1^1,s2); continue; } //printf("s1 %d\n a1 %d\ns2 %d\n a2 %d\n",s1,a1,s2,a2); solver.add_clause(a1^1,s1); solver.add_clause(s1^1,a1); solver.add_clause(a2^1,s2); solver.add_clause(s2^1,a2); solver.add_clause(a1^1,a2); solver.add_clause(s1^1,s2); solver.add_clause(a2^1,a1); solver.add_clause(s2^1,s1); } if(solver.solve())printf("Yes\n"); else printf("No\n"); } return 0; }