http://acm.hdu.edu.cn/showproblem.php?pid=1811
Test instructions
Problem description Since Lele developed the rating system, his Tetris career is even more powerful, he soon all over the game to the WORLD.
To better match the preferences of those enthusiasts, Lele a new idea: he will make a global Tetris Master rankings, regular updates, more than the Forbes rich List. about how to rank, this needless to know is according to rating from high to low to row, if two people have the same rating, then according to these People's RP from high to low to ROW.
finally, Lele to begin to act, ranking n individuals. For convenience, everyone has been numbered, from 0 to N-1, and the larger the number, the higher the Rp.
Meanwhile Lele obtained some (M) information about rating from the Paparazzi. This information may have three cases, namely "a > b", "a = b", "a < b", respectively, that the rating of a is higher than b, equal to B and less than B.
Now Lele not let you to help him make this master list, he just want to know, according to whether this information can determine the master list, yes, the output "ok". otherwise, you can determine the cause of the error because the information is incomplete (output "uncertain"), or because the information contains a conflict (output "CONFLICT").
Note that if the information contains both conflicting and incomplete information, the output is "CONFLICT".
Ideas:
This topic needs to pay attention to the existence of the "=" situation, the existence of the equal sign is equivalent to the two are a whole, they can not be inserted into the score of different people, so use and check set and together, and then unified with root to replace the whole Can.
Then there is the general topological sort.
1#include <iostream>2#include <algorithm>3#include <cstring>4#include <cstdio>5#include <sstream>6#include <vector>7#include <stack>8#include <queue>9#include <cmath>Ten#include <map> one#include <Set> a using namespacestd; -typedefLong Longll; -typedef pair<int,int>pll; the Const intINF =0x3f3f3f3f; - Const intMAXN =10000+5; - - intn,m; + intpre_n; - intp[maxn]; + intvis[maxn]; a intdegree[maxn]; at Charc[2*maxn]; - intu[2*maxn],v[2*maxn]; - BOOLflag1,flag2; -vector<int>g[maxn]; - - intFind (intx) in { - returnp[x]==x?x:p[x]=Find (p[x]); to } + - voidTuopu () the { *Memset (vis,0,sizeof(vis)); $ for(intI=1; i<=n;i++)Panax Notoginseng { - intpos=-1; the intnum=0; + for(intj=0; j<pre_n;j++) a { the intu=Find (j); + if(vis[u]!=i && degree[u]==0) - { $vis[u]=i; $pos=u; -num++; - } the } - if(num>1) flag1=true;Wuyi if(pos==-1) {flag2=true;return;} thedegree[pos]=-1; - for(intj=0; J<g[pos].size (); J + +) wu { - intv=g[pos][j]; aboutdegree[v]--; $ } - } - return; - } a + intMain () the { - //freopen ("in.txt", "r", stdin); $ while(~SCANF ("%d%d",&n,&M)) the { thepre_n=n; the for(intI=0; i<n;i++) g[i].clear (); theMemset (degree,0,sizeof(degree)); - for(intI=0; I<n;i++) p[i]=i; in the for(intI=0; i<m;i++) the { aboutCin>>u[i]>>c[i]>>v[i]; the if(c[i]=='=') the { the intx=Find (u[i]); + inty=Find (v[i]); - if(x!=Y) the {Bayip[x]=y; then--; the } - } - } the the for(intI=0; i<m;i++) the { the intx=Find (u[i]); - inty=Find (v[i]); the if(c[i]=='>') the { the G[x].push_back (y);94degree[y]++; the } the Else if(c[i]=='<') the {98 G[y].push_back (x); aboutdegree[x]++; - }101 }102 103Flag1=flag2=false;104 Tuopu (); the if(flag2) puts ("CONFLICT");106 Else if(flag1) puts ("uncertain");107 ElsePuts"OK");108 }109 return 0; the}
HDU 1811 rank of Tetris (topological sorting + and Collection)