Loading

HDU 1811:Rank of Tetris(并查集+拓扑排序)

http://acm.hdu.edu.cn/showproblem.php?pid=1811

 

Rank of Tetris

 

Problem Description
 
自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球。
为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球Tetris高手排行榜,定时更新,名堂要比福布斯富豪榜还响。关于如何排名,这个不用说都知道是根据Rating从高到低来排,如果两个人具有相同的Rating,那就按这几个人的RP从高到低来排。
终于,Lele要开始行动了,对N个人进行排名。为了方便起见,每个人都已经被编号,分别从0到N-1,并且编号越大,RP就越高。 同时Lele从狗仔队里取得一些(M个)关于Rating的信息。这些信息可能有三种情况,分别是"A > B","A = B","A < B",分别表示A的Rating高于B,等于B,小于B。
现在Lele并不是让你来帮他制作这个高手榜,他只是想知道,根据这些信息是否能够确定出这个高手榜,是的话就输出"OK"。否则就请你判断出错的原因,到底是因为信息不完全(输出"UNCERTAIN"),还是因为这些信息中包含冲突(输出"CONFLICT")。 注意,如果信息中同时包含冲突且信息不完全,就输出"CONFLICT"。
 
Input
 
本题目包含多组测试,请处理到文件结束。 每组测试第一行包含两个整数N,M(0<=N<=10000,0<=M<=20000),分别表示要排名的人数以及得到的关系数。 接下来有M行,分别表示这些关系
 
Output
 
对于每组测试,在一行里按题目要求输出
 
Sample Input
 
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
 
Sample Output
 
OK
CONFLICT
UNCERTAIN
 
  1 #include <cstdio>
  2 #include <algorithm>
  3 #include <cstring>
  4 #include <iostream>
  5 #include <queue>
  6 using namespace std;
  7 #define N 10010
  8 int x[N*2],y[N*2];
  9 char ch[N*2][3];
 10 int fa[N],deg[N];
 11 int f1,f2,tot;
 12 struct node
 13 {
 14     int next,to;
 15 }edge[N*2];
 16 int head[N],n,m,cnt;
 17 /*
 18 并查集+拓扑排序
 19 因为有等于的情况,把等于的情况放在一个集合里面,拓扑排序的时候
 20 只用那个集合里面的一个根当做集合中的所有元素进行排序。
 21 剩下的就是拓扑排序的内容了:
 22 如果有回路,那么入队的点数(更新后入度为0)是<n的,
 23 这里要注意在等于的情况下,也要算是一个点。
 24 关系确定的情况,是每次入队的时候都只会入一个入度为零的点。
 25 */
 26 void init()
 27 {
 28     f1=f2=1;//f1 = CONFICT , f2 = UNCERTAIN
 29     tot=0;
 30     cnt=0;
 31     memset(deg,0,sizeof(deg));
 32     memset(head,-1,sizeof(head));
 33     for(int i=0;i<n;i++){
 34         fa[i]=i;
 35     }
 36 }
 37 
 38 int Find(int x)
 39 {
 40     if(x==fa[x]) return x;
 41     return fa[x]=Find(fa[x]);
 42 }
 43 
 44 void Merge(int x,int y)
 45 {
 46     int fx=Find(x),fy=Find(y);
 47     if(fx!=fy){
 48         fa[fx]=fy;
 49     }
 50 }
 51 
 52 void add(int u,int v)
 53 {
 54     edge[tot].to=v;
 55     edge[tot].next=head[u];
 56     head[u]=tot++;
 57 }
 58 
 59 void topper()
 60 {
 61     queue<int> que;
 62     while(!que.empty()) que.pop();
 63     for(int i=0;i<n;i++){
 64         if(deg[i]==0&&fa[Find(i)]==i){
 65              que.push(i);
 66              cnt++;
 67         }
 68     }
 69     while(!que.empty()){
 70         int top=que.front();que.pop();
 71         if(!que.empty()) f2=0;
 72         for(int k=head[top];~k;k=edge[k].next){
 73             int v=edge[k].to;
 74             deg[v]--;
 75             if(deg[v]==0){
 76                 que.push(v);
 77                 cnt++;
 78             }
 79         }
 80     }
 81     if(cnt<n) f1=0;
 82 }
 83 
 84 int main()
 85 {
 86     while(~scanf("%d%d",&n,&m)){
 87         init();
 88         for(int i=0;i<m;i++){
 89             scanf("%d%s%d",&x[i],ch[i],&y[i]);
 90             if(ch[i][0]=='='){
 91                 cnt++;
 92                 Merge(x[i],y[i]);
 93             }
 94         }
 95         for(int i=0;i<m;i++){
 96             if(ch[i][0]=='=') continue;
 97             int fx=Find(x[i]),fy=Find(y[i]);
 98             if(fx==fy) f1=0;
 99             if(ch[i][0]=='<'){
100                 add(fx,fy);
101                 deg[fy]++;
102             }
103             else{
104                 add(fy,fx);
105                 deg[fx]++;
106             }
107         }
108         topper();
109         if(f1==1&&f2==1) cout<<"OK"<<endl;
110         else if(f1==0||f1==0&&f2==0) cout<<"CONFLICT"<<endl;
111         else cout<<"UNCERTAIN"<<endl;
112     }
113     return 0;
114 }

 

posted @ 2016-06-27 21:07  Shadowdsp  阅读(205)  评论(0编辑  收藏  举报