Rank of Tetris
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8451 Accepted Submission(s): 2415
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则说明拓扑排序序列不唯一
a —> b a—> c b c 排序未知
②如果排序的总个数小于给定的个数,则说明存在回路
a —> b b —> a a b 的入度永远为 1
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define M 1000010
#define INF 510
#define RCL(a, b) memset(a, b, sizeof(a))
int n, m, indegree[M], pre[M];
struct node
{
int to, next;
};
struct node2
{
int a, b;
char ch[2];
};
node2 nh[M];
node edge[M];
int head[M], edgenum, ans[M], sum;
bool vis[M], okc, okw;
void addedge(int a, int b)
{
node e = {b, head[a]};
edge[edgenum] = e;
head[a] = edgenum++;
}
int find(int a)
{
int r = a;
while(r != pre[r])
r = pre[r];
return r;
}
void join(int x, int y)
{
int fx = find(x);
int fy = find(y);
if(fx != fy)
{
pre[fy] = fx;
}
}
void topo()
{
int top;
queue<int> q;
for(int i=0; i<n; i++)
{
int t = find(i);
if(!indegree[t] && i == t)
{
q.push(i);
}
}
while(!q.empty())
{
sum--;
top = q.front();
q.pop();
if(!q.empty()) okw = true;//如果去掉一个还不是空的话,表示里面有多余的,表示信息表达不完全
//正常的话是一个连着一个的 1 ---> 2 ---> 3 --- 4,而队列非空就表示有两个在相同位置 1 ---> 2 && 1---> 3
for(int i=head[top]; i!=-1; i=edge[i].next)
{
indegree[edge[i].to]--;
if(!indegree[edge[i].to])
{
q.push(edge[i].to);
}
}
}
if(sum > 0)
{
okc = true;
}
if(okc)
{
printf("CONFLICT\n");
}
else if(okw)
{
printf("UNCERTAIN\n");
}
else
{
printf("OK\n");
}
}
void init()
{
RCL(head, -1);
edgenum = 0;
RCL(indegree, 0);
okc = false;//冲突
okw = false;//完全
for(int i=0; i<=n; i++)
{
pre[i] = i;
}
sum = n;
}
int main()
{
char ch[2];
while(scanf("%d%d", &n, &m) != EOF)
{
init();
for(int i=0; i<m; i++)
{
scanf("%d%s%d", &nh[i].a, nh[i].ch, &nh[i].b);
if(nh[i].ch[0] == '=')
{
join(nh[i].a, nh[i].b);//并查集用来合并相同的
sum--;//总数减少
}
}
for(int i=0; i<m; i++)
{
if(nh[i].ch[0] == '=') continue;//相等已经提前考虑
int fa = find(nh[i].a);
int fb = find(nh[i].b);
if(fa == fb)//两个人的相等的情况是不会有大小之比的
{
okc = true;
}
if(nh[i].ch[0] == '<')
{
addedge(fb, fa);
indegree[fa]++;
}
else
{
addedge(fa, fb);
indegree[fb]++;
}
}
topo();
}
return 0;
}