题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4879
代码:
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> using namespace std; const int maxn = 550; struct Two_Sat { int n; vector<int> G[maxn*2]; bool mark[maxn*2]; int s[maxn*2],cnt; void init(int n) { this->n = n; memset(mark,0,sizeof(mark)); for(int i=0; i<=n*2; i++) G[i].clear(); } void add_clause(int u,int v,int flag,char symbol) { u = u*2 + flag; v = v*2 + flag; if(symbol == 'A') { if(flag) { G[u].push_back(v); G[v].push_back(u); G[u^1].push_back(u); G[v^1].push_back(v); } else { G[u^1].push_back(v); G[v^1].push_back(u); } } else if(symbol == 'O') { if(flag) { G[u^1].push_back(v); G[v^1].push_back(u); } else { G[u].push_back(v); G[v].push_back(u); G[u^1].push_back(u); G[v^1].push_back(v); } } else { if(flag) { G[u^1].push_back(v); G[v].push_back(u^1); G[v^1].push_back(u); G[u].push_back(v^1); } else { G[u].push_back(v); G[v].push_back(u); G[u^1].push_back(v^1); G[v^1].push_back(u^1); } } } bool dfs(int u) { if(mark[u^1]) return false; if(mark[u]) return true; mark[u] = true; s[cnt++] = u; for(int i=0; i<G[u].size(); i++) { if(!dfs(G[u][i])) return false; } return true; } bool solve() { for(int i=0; i<n*2; i+=2) { if(!mark[i] && !mark[i+1]) { cnt = 0; if(!dfs(i)) { while(cnt>0) mark[s[--cnt]] = false; if(!dfs(i+1)) return false; } } } return true; } } solver; int main() { //freopen("E:\acm\input.txt","r",stdin); int N; while(cin>>N) { int b[505][505]; bool ans = true; for(int i=0; i<N; i++) for(int j=0; j<N; j++) scanf("%d",&b[i][j]); for(int i=0; i<N; i++) for(int j=i; j<N; j++) { if(i == j && b[i][j] != 0) { ans = false; break; } if(b[i][j] != b[j][i]) { ans = false; break; } } if(!ans) { printf("NO "); continue; } for(int k=0; k<31; k++) { solver.init(N); for(int i=0; i<N; i++) for(int j=i+1; j<N; j++) { char symbol; if(i%2 == 1 && j%2 == 1) symbol = 'O'; else if(i%2 == 0 && j%2 == 0) symbol = 'A'; else symbol = 'X'; if(b[i][j] & (1<<k)) solver.add_clause(i,j,1,symbol); else solver.add_clause(i,j,0,symbol); } if(!solver.solve()) { ans = false; break; } } if(ans) printf("YES "); else printf("NO "); } }