HDU 1811 Rank of Tetris 【拓扑排序 + 并查集】
自从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.合法输出OK
2.有环,这里注意,可能有相等的情况,那么把相等的两个数合并,然后用他们的根作为代表当作点,合并一次,n的总数就-1
3.不符合拓扑唯一性,那么压入队列时队列大小>1说明有不同的入度为0的点,那么标记一下
【代码】:
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,n,x) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
using namespace std;
const int maxn = 10010;
const int mod = 142857;
int n,m,num;
queue<int> q;
vector<int> G[20010];
char c[20010];
int inDeg[10010],fa[10010];
int a[20010],b[20010];
void init()
{
for(int i=0;i<=n;i++)
{
fa[i]=i;
G[i].clear();
}
}
int Find(int x)
{
if(fa[x] == x) return x;
return fa[x]=Find(fa[x]);
}
int join(int x,int y)
{
int fx=Find(x);
int fy=Find(y);
if(fx==fy) return 0;
if(fx!=fy)
{
fa[fy]=fx;
}
return 1;
}
void topSort()
{
int ok=0;
while(!q.empty()) q.pop();//
for(int i=0; i<n; i++) if(!inDeg[i] && Find(i)==i) q.push(i); //入度为0且是根节点的时候才压入队列
while(!q.empty())
{
if(q.size()>1) ok=1;
int now = q.front(); q.pop();
num--;
for(int i=0;i<G[now].size();i++)
{
int nxt = G[now][i];
if(--inDeg[nxt] == 0)
{
q.push(nxt);
}
}
}
if(num>0) printf("CONFLICT\n");
else if(ok) printf("UNCERTAIN\n");
else printf("OK\n");
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
memset(inDeg,0,sizeof(inDeg));
num=n;
for(int i=0;i<m;i++)
{
scanf("%d %c %d",&a[i],&c[i],&b[i]);
if(c[i] == '=') //预处理相等的情况
{
if(join(a[i],b[i])) //一直在这里WA掉却不知道,因为只有根节点不同才合并
num--;
}
}
for(int i=0;i<m;i++)
{
if(c[i]=='=') continue;
int x=Find(a[i]);
int y=Find(b[i]);
if(c[i] == '>')
{
G[x].push_back(y);
inDeg[y]++;
}
if(c[i] == '<')
{
G[y].push_back(x);
inDeg[x]++;
}
}
topSort();
}
}
HDU 1811 Rank of Tetris 【拓扑排序 + 并查集】的更多相关文章
- ACM: hdu 1811 Rank of Tetris - 拓扑排序-并查集-离线
hdu 1811 Rank of Tetris Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- hdu 1811 Rank of Tetris - 拓扑排序 - 并查集
自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球Tetris高手排行榜, ...
- hdu1811 Rank of Tetris 拓扑排序+并查集
这道题是拓扑排序和并查集的综合运用. 由于排行榜是一种从高到低的排序.所以在拓扑排序的时候,如果有一次加入的入度为零的点数大于1,就有变得不确定了(UNCERTAIN). 由于只有一棵树,当树的数量大 ...
- Rank of Tetris 拓扑排序+并查集
Problem Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子: ...
- hdu 1811 Rank of Tetris (拓扑 & 并查集)
Rank of Tetris Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 1811 拓扑排序 并查集
有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...
- HDU 1811 Rank of Tetris(并查集按秩合并+拓扑排序)
Rank of Tetris Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 1811 Rank of Tetris(拓扑排序+并查集)
题目链接: 传送门 Rank of Tetris Time Limit: 1000MS Memory Limit: 32768 K Description 自从Lele开发了Rating系统, ...
- HDU 1811 Rank of Tetris(并查集+拓扑排序 非常经典)
Rank of Tetris Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 1811 Rank of Tetris (并查集+拓扑排序)
Rank of Tetris Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
随机推荐
- [洛谷P4779]【模板】单源最短路径(标准版)
题目大意:单元最短路径(卡$SPFA$) 题解:$dijkstra$($\underline{\hspace{0.5em}}\underline{\hspace{0.5em}}gnu\underlin ...
- 【BZOJ 2957】楼房重建&&Codechef COT5 Count on a Treap&&【NOIP模拟赛】Weed 线段树的分治维护
线段树是一种作用于静态区间上的数据结构,可以高效查询连续区间和单点,类似于一种静态的分治.他最迷人的地方在于“lazy标记”,对于lazy标记一般随我们从父区间进入子区间而下传,最终给到叶子节点,但还 ...
- 前缀统计 [Trie]
前缀统计 描述 给定N个字符串S1,S2...SN,接下来进行M次询问,每次询问给定一个字符串T,求S1-SN中有多少个字符串是T的前缀.输入字符串的总长度不超过10^6,仅包含小写字母. 输入格式 ...
- C语言一些常用的功能
1.测试运行时间: #include<stdio.h> #include<stdlib.h> #include<time.h> int main() { clock ...
- Codeforces Round #525 (Div. 2)A. Ehab and another construction problem
A. Ehab and another construction problem 题目链接:https://codeforc.es/contest/1088/problem/A 题意: 给出一个x,找 ...
- scala(一种静态语言)
语法: 关键字 val(表示:值) 不可变 ex: val a:Int=1 或者 val a=1(会自动识别类型,无基本类与包装类之分) 输出:a:Int=1 关键字var ex: var a ...
- source改变当前路径
转摘自:http://hi.baidu.com/homappy/item/90e416525d2faf958c12edb7 Shell 脚本执行有三种方法 bash 脚本名 sh 脚本名 chmod ...
- oracle 包和包实现
包: create or replace package sp_pexam_clear as --定义结构体 /*type re_stu is record( rname student.name%t ...
- 【转载】字符串最小表示法-O(n)算法
原博客链接:http://blog.csdn.net/zy691357966/article/details/39854359 未授权,侵权删. 因为这篇博客写得真好..转载了.. 红色的字是原博主写 ...
- 【BZOJ3624】【APIO2008】免费道路 [生成树][贪心]
免费道路 Time Limit: 2 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description Input Output Sampl ...