hdu 1811 Rank of Tetris(拓扑排序+并查集)
来源:互联网 发布:电子日记本软件 编辑:程序博客网 时间:2024/05/24 04:26
自从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
给你一些编号的大小关系,问你能否对所有的编号排出大小顺序,如果能输出OK,如果给出的关系自相矛盾输出CONFLICT,如果信息不全输出UNCERTAIN. 如果仅有大于小于的话: 对于一种关系建立有向图,然后进行拓扑排序,每次入度为0的点入队,如果队中大于一个点,说明这些点之间没有边,即这些点不能判断大小,信息不全。如果发现有环,说明冲突。
加上等于关系,如果还像原来一样,一个数等于另一个数,那样需要找到另一个数和其他数的大小关系,然后用这个数建边,这样就会非常麻烦,这里我们把所有的相等的点选出一个点来代替他们(即并查集),点的总数相应减少就可以了
#include <stdio.h>#include <string.h>#include <algorithm>#include <math.h>#include <queue>using namespace std;#define maxn 11000#define maxm 21000int n,m;int head[maxn],pre[maxn],indeg[maxn],vis[maxn];char s[maxm];int a[maxm],b[maxm];int tot;struct Edge{ int to,next;} edge[maxm];int findroot(int num){ while(pre[num] != num) { num = pre[num]; } return pre[num];}void add(int u,int v){ edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot++; indeg[v]++;}int topo(){ queue<int>q; int ans = 1; int num = 0; int block = 0; for(int i = 0; i < n; i++) { if(findroot(i) == i) { block++; } } for(int i = 0; i < n; i++) { int temp = findroot(i); if(indeg[temp] == 0 &&vis[temp] == 0) { q.push(temp); vis[temp] = 1; } } while(!q.empty()) { if(q.size() >= 2) { ans = -1; } int temp = q.front(); q.pop(); num++; for(int i = head[temp]; i != -1; i = edge[i].next) { int x = findroot(edge[i].to); indeg[x]--; if(indeg[x] == 0 && vis[x] == 0) { q.push(x); vis[x] = 1; } } } if(num < block) ans = 0;//冲突 return ans;}int main(){ while(~scanf("%d%d",&n,&m)) { tot = 0; int ans = 4; memset(head,-1,sizeof(head)); memset(indeg,0,sizeof(indeg)); memset(vis,0,sizeof(vis)); for(int i = 0; i < n; i++) { pre[i] = i; } for(int i = 0; i < m; i++) { scanf("%d %c%d",&a[i],&s[i],&b[i]); if(s[i] == '=') { int roota = findroot(a[i]); int rootb = findroot(b[i]); if(roota != rootb) { pre[roota] = rootb; } } } for(int i = 0; i < m; i++) { int u = findroot(a[i]), v = findroot(b[i]); if(u == v && s[i] != '=') { ans = 0; break; } if(s[i] == '<') add(u,v); if(s[i] == '>') add(v,u); } if(ans != 0) ans = topo(); if(ans == 1) printf("OK\n"); if(ans == -1) printf("UNCERTAIN\n"); if(ans == 0) printf("CONFLICT\n"); }}
- hdu 1811Rank of Tetris (并查集+拓扑排序)
- HDU 1811 Rank of Tetris(拓扑排序+并查集)
- HDU 1811 Rank of Tetris(拓扑排序+并查集)
- hdu-1811-Rank of Tetris-并查集+拓扑排序
- hdu 1811 Rank of Tetris (并查集+拓扑排序)
- hdu 1811 Rank of Tetris (并查集 + 拓扑排序)
- HDU 1811 Rank of Tetris(并查集+拓扑排序)
- 【HDU】1811 Rank of Tetris 并查集+拓扑排序
- HDU 1811-Rank of Tetris(拓扑排序+并查集)
- HDU 1811 Rank of Tetris(并查集+拓扑排序)
- hdu 1811 Rank of Tetris 【并查集+拓扑排序】
- hdu 1811 Rank of Tetris 并查集+拓扑排序
- hdu 1811 Rank of Tetris 拓扑排序+并查集
- HDU 1811 Rank of Tetris -- 并查集+拓扑排序
- [HDU 1811] Rank of Tetris (并查集+拓扑排序)
- HDU 1811 Rank of Tetris (并查集+拓扑排序)
- [HDU 1811]Rank of Tetris[拓扑排序][并查集]
- hdu 1811 Rank of Tetris 拓扑排序+并查集
- 3.1 分析并写出下列程序的运行结果。
- Xcode报错undefined xxx
- 筛法判素数(大数)
- AngularJS综合用户信息, 表单验证,过滤查询,全选反选,批量删除,修改,添加
- Android三种姿势带你玩转360度全景图功能
- hdu 1811 Rank of Tetris(拓扑排序+并查集)
- HDU 5919 Sequence II
- Angularjs+路由表格的增删改查综合
- python设计模式之设计模式简介
- hadoop概念-MapReduce数据流
- 正向代理和Nginx反向代理配置介绍
- android studio 日期选择DatePickerDialog取值
- Otsu算法原理及实现
- Spring装配Bean之组件扫描和自动装配