This is a comprehensive application of topological sorting and query sets.
The ranking is a sort from high to low. Therefore, in Topology Sorting, if the number of points with zero input degree is greater than 1, it becomes uncertain ).
Because there is only one tree, when the number of trees is greater than 1, there is a conflict. Another possibility is that when A> B (or a <B) is input, the parent nodes in the query set are the same. Because a set of questions is actually considered as a point. In this case, the inequality is a conflict.
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N = 10010, M =20010;struct node{ int to, next;};node edge[M];int ind[N], head[N],que[N],f[N],L[N],R[N];int iq,tot,num,FLAG;char ch[N];int Find(int x){ if(x==f[x]) return x; return f[x]=Find(f[x]);}void topo(int n){ int i,k,j=0,s; for(i=0;i<n;i++) if(ind[i]==0&&i==Find(i)) que[j++]=i; FLAG=0; if(j>1) FLAG=1; s=j; for(i=0;i<j;i++) { if(j-s>1) FLAG=1; s=j; int u=que[i]; num--; for(k=head[u]; k!=-1; k=edge[k].next) { ind[edge[k].to]--; if(ind[edge[k].to]==0) que[j++]=edge[k].to; } } iq=j;}void addedge(int i,int j){ edge[tot].to=j;edge[tot].next=head[i];head[i]=tot++;}void Link(int i,int j){ int a=Find(i),b=Find(j); if(a!=b) {f[b]=a;}}void init(){ tot=0; for(int i=0;i<N;i++) { f[i]=i; head[i]=-1; ind[i]=0; }}int main(){ //freopen("test.txt","r",stdin); int n,m,i,j,k; while(scanf("%d%d",&n,&m)!=EOF) { init(); int flag=1; num=n; for(i=0;i<m;i++) { scanf("%d %c %d",&L[i],&ch[i],&R[i]); if(ch[i]==‘=‘) {Link(L[i],R[i]);num--;} } for(i=0;i<m;i++) { if(ch[i]==‘=‘) continue; int a=Find(L[i]),b=Find(R[i]); if(a==b) flag=0; if(ch[i]==‘>‘) {addedge(a,b); ind[b]++;} else {addedge(b,a); ind[a]++;} } topo(n); if(num>1||!flag) printf("CONFLICT\n"); else if(FLAG) printf("UNCERTAIN\n"); else printf("OK\n"); } return 0;}
Hdu1811 rank of RIS