Rank of RIS
Time Limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 5221 accepted submission (s): 1465
Problem description Since Lele developed the rating system, his Tetris career has become even more powerful, and soon he pushed the game to the world.
To better suit those fans' preferences, Lele came up with a new idea: he will create a global ranking of Tetris experts, which will be updated on a regular basis and the name hall will be better than the Forbes ranking. We don't know how to rank based on rating from high to low. If two people share the same rating, then sort by the RP of these people from high to low.
Finally, Lele is about to take action and rank n people. For convenience, everyone has been numbered, from 0 to N-1, and the larger the number, the higher the RP.
At the same time, Lele obtained some (m) rating information from the paparazzi. There are three possible conditions for such information: "A> B", "A = B", and "A <B", respectively, indicating that the rating of A is higher than B and equal to B, smaller than B.
Now, Lele doesn't ask you to help him create the master list. He just wants to know whether the master list can be determined based on the information. If yes, "OK" is output ". Otherwise, determine the cause of the error, whether it is because the information is incomplete (output "uncertain") or because the information contains conflicts (output "Conflict ").
Note: If the information contains conflicts and the information is incomplete, "Conflict" is output ".
Input this question contains multiple groups of tests, please process until the end of the file.
The first line of each test contains two integers, N, and M (0 <= n <= strong, 0 <= m <= 20000), indicating the number of people to be ranked and the number of relationships obtained.
Next there are m rows, indicating the relationships respectively
Output for each group of tests, output according to the requirements of the question in one row
Sample Input
3 30 > 11 < 20 > 24 41 = 21 > 32 > 00 > 13 31 > 01 > 22 < 1
Sample output
OKCONFLICTUNCERTAIN
Authorlinle determines whether there is a ring in topological sorting. If there is no ring, it depends on whether there are multiple solutions (that is, there are more than two vertices at the same level as 0). If there is a unique solution, there is an equal relation in the output "OK". If the difference is greater than or less than the same, it is easy to say, but if it is equal, it is necessary to use and query the set to merge. At the beginning, I saved all equal relations, finally, after the input is complete, the points connected to the two points are respectively placed behind them, and then .. MLE. If the query set is used, this problem is well solved. It can omit many repeated points. For example, if 1 = 2 2 = 3, the three points are equivalent to one point, that is, in the same set, this saves memory for processing ..
#include <stdio.h>#include <string.h>#include <math.h>#include <stdlib.h>#include <iostream>#include <algorithm>#include <vector>#include <stack>using namespace std;int n,m,in[10010],fa[10010],cnt,u[20010],v[20010];char c[20010];vector <int> eg[20010];void make_set(){ for(int i=0;i<n;i++) { fa[i]=i; eg[i].clear(); }}int Find(int x){ if(x!=fa[x]) fa[x]=Find(fa[x]); return fa[x];}void Union(int x,int y){ int fx=Find(x); int fy=Find(y); if(fx!=fy) { fa[fy]=fx; cnt++; }}void top_sort(){ stack <int> s; int flag=0; for(int i=0;i<n;i++) if(in[i]==0&&fa[i]==i) s.push(i); while(!s.empty()) { if(s.size()>1)flag=1; int u=s.top();s.pop(); cnt++; for(int i=0;i<eg[u].size();i++) { int v=eg[u][i]; in[v]--; if(in[v]==0) s.push(v); } } if(cnt<n) puts("CONFLICT"); else if(flag==1) puts("UNCERTAIN"); else puts("OK");}int main(){ while(~scanf("%d%d",&n,&m)) { make_set(); memset(in,0,sizeof(in));cnt=0; for(int i=0;i<m;i++) { scanf("%d %c %d",&u[i],&c[i],&v[i]); if(c[i]=='=') Union(u[i],v[i]); } for(int i=0;i<m;i++) { if(c[i]=='=')continue; int a=Find(u[i]),b=Find(v[i]); if(c[i]=='>') { eg[a].push_back(b); in[b]++; } else { eg[b].push_back(a); in[a]++; } } top_sort(); } return 0;}
HDU 1811-rank of RIS (Topology Sorting + query set)