3 3 0 > 1 1 < 2 0 > 2 4 4 1 = 2 1 > 3 2 > 0 0 > 1 3 3 1 > 0 1 > 2 2 < 1
OK CONFLICT UNCERTAIN
#include<iostream> #include<vector> #include<stdio.h> #include<string.h> using namespace std; const int N = 10005; int indu[N],n,fath[N],idk; vector<int>map[N]; void init() { for(int i=0;i<=n;i++) { indu[i]=0,fath[i]=i; map[i].clear(); } idk=0; } int findfath(int x) { if(x==fath[x]) return fath[x]; fath[x]=findfath(fath[x]); return fath[x]; } void setfath(int x,int y) { x=findfath(x); y=findfath(y); fath[x]=y; } void topu() { int k=0,s[N],uncertain=0,node; for(int i=0;i<n;i++) { if(fath[i]==i) idk++; if(indu[i]==0&&fath[i]==i)//fath[i]==i是用于判断非等号关第的点,除了最终父节点,可以看作是等号点的集合 s[k++]=i; } while(k--) { if(k>0) uncertain=1; idk--; node=s[k]; for(int i=0;i<map[node].size();i++) { int t=map[node][i]; indu[t]--; if(indu[t]==0) s[k++]=t; } } if(idk>0) printf("CONFLICT\n"); else if(uncertain) printf("UNCERTAIN\n"); else printf("OK\n"); } int main() { int a[N],b[N],m; char f[N]; while(scanf("%d%d",&n,&m)>0) { init(); for(int i=0;i<m;i++) { scanf("%d %c %d",&a[i],&f[i],&b[i]); if(f[i]=='=') setfath(a[i],b[i]); } for(int i=0;i<m;i++) if(f[i]!='=') { a[i]=findfath(a[i]); b[i]=findfath(b[i]); if(f[i]=='>') { indu[b[i]]++; map[a[i]].push_back(b[i]); } else { indu[a[i]]++; map[b[i]].push_back(a[i]); } } topu(); } }
HDU1811Rank of Tetris(并查集+拓扑排序)
原文地址:http://blog.csdn.net/u010372095/article/details/44107779