标签:
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7961 Accepted Submission(s): 2266
#include <stdio.h> #include <algorithm> #include <string.h> #include <queue> using namespace std; const int N =10005; int n,m; int father[N]; int indegree[N]; struct Node{ int a,b; char s; }node[2*N]; struct Edge{ int u,v,next; }edge[2*N]; int head[N]; int _find(int x){ if(x!=father[x]) father[x] = _find(father[x]); return father[x]; } void Union(int a,int b){ int x = _find(a); int y = _find(b); if(x==y) return ; father[x] = y; } void addedge(int u,int v,int &k){ edge[k].u = u,edge[k].v = v; edge[k].next = head[u],head[u]=k++; } int main() { while(scanf("%d%d",&n,&m)!=EOF){ for(int i=0;i<n;i++) { father[i] = i; head[i] = -1; indegree[i] = 0; } bool flag = true,flag1 = true; ///flag判断冲突,flag1判断完整性 int cnt = n; for(int i=0;i<m;i++){ scanf("%d %c %d",&node[i].a,&node[i].s,&node[i].b); if(node[i].s==‘=‘){ Union(node[i].a,node[i].b); cnt--; } } int tot = 0; for(int i=0;i<m;i++){ char s = node[i].s; int a = node[i].a,b = node[i].b; int x = _find(a); int y = _find(b); if(s!=‘=‘&&x==y) flag = false; if(s==‘<‘){ indegree[x]++; addedge(y,x,tot); } if(s==‘>‘){ indegree[y]++; addedge(x,y,tot); } } queue<int> q; for(int i=0;i<n;i++){ if(indegree[i]==0&&father[i]==i){ q.push(i); } } while(!q.empty()){ int t = q.front(); q.pop(); cnt--; if(!q.empty()) flag1 = false; ///存在多个入度为0的点,答案不唯一 for(int k = head[t];k!=-1;k=edge[k].next){ int v = edge[k].v; indegree[v]--; if(indegree[v]==0) q.push(v); } } if(cnt>0) flag = false; if(!flag) printf("CONFLICT\n"); else if(!flag1) printf("UNCERTAIN\n"); else printf("OK\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5486241.html