这道题不看书上的分析还真不知道怎么做,关键就是转化成图论,然后利用拓扑排序判断DAG来做。
另外一个值得学习的地方是编号的时候,A+和A-可分别变为2n+1和2n,然后一个重要的关系要利用好就是(2n+1)^1 = 2n,2n^1 = (2n+1),可以很容易的进行A+和A-的变换。
#include <bits/stdc++.h> #define maxn 64 using namespace std; int n, t, Road[maxn][maxn], vis[maxn]; inline int ID(const char x, const int y){ return y=='+' ? (x-'A')*2+1 : (x-'A')*2; } bool DFS(int u) { vis[u] = -1; for(int v = 0; v < 52; ++v) if(Road[u][v]){ if(vis[v] == -1) return false; else if(!vis[v] && !DFS(v)) return false; } vis[u] = 1; return true; } bool topsort() { for(int i = 0; i < 52; ++i) if(!vis[i] && !DFS(i)) return false; return true; } int main() { ios::sync_with_stdio(false); while(cin >> n){ memset(Road, 0, sizeof(Road)); memset(vis, 0, sizeof(vis)); while(n--){ char str[32]; cin >> str; for(int i = 0; i < 4; ++i) for(int j = 0; j < 4; ++j) if(i != j && str[i*2]!='0' && str[j*2]!='0') Road[ID(str[i*2], str[i*2+1])^1][ID(str[j*2], str[j*2+1])] = 1; } if(topsort()) puts("bounded"); else puts("unbounded"); } return 0; }