您的位置:首页 > 其它

hdu 1811 Rank of Tetris 拓扑排序+并查集

2016-02-08 12:32 295 查看
拓扑排序的过程中,根据队列中是否出现多个入度为0的点,可以判断出信息是否完全。

根据拓扑排序访问过的点数,可以判断出是否存在环,也就是信息之间是否存在矛盾。


Rank of Tetris

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 7519    Accepted Submission(s): 2147


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

 

Author

linle

 

Source

HDOJ 2007 Summer Exercise(2)

 

/**==========================================
* This is a solution for ACM/ICPC problem
*
* @source:hdu 1811
* @type:
* @author: wust_ysk
* @blog: http://blog.csdn.net/yskyskyer123 * @email: 2530094312@qq.com
*===========================================*/
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
const int INF =0x3f3f3f3f;
const int maxn=10000 ;
const int maxm=20000 ;
int n,m;
//const int maxV=12 ;
int pre[maxn+5];
vector<int> G[maxn+5];
int in[maxn+5];
struct Query
{
int x,y;
char op;
Query(){}
Query(int x,char op,int y):x(x),op(op),y(y){}

} q[maxm+5];

bool conflict,uncertain;

int find(int x)
{
return pre[x]==x?x:pre[x]=find(pre[x]);
}
void merge(int x,int y)
{
int rootx=find(x);
int rooty=find(y);
if(rootx==rooty) return;
pre[rootx]=rooty;
}

void init()
{
uncertain=conflict=0;
for(int i=0;i<n;i++) G[i].clear();
for(int i=0;i<n;i++) pre[i]=i,in[i]=0;
}

inline void add_edge(int x,int y)
{
G[x].push_back(y);
in[y]++;
}

void topsort()
{
queue<int >q;
int num=0;

for(int i=0;i<n;i++) if(find(i)==i&&!in[i])
{
num++;
q.push(i);
}
while(!q.empty())
{
if(q.size()>1) uncertain=1;
int x=q.front();q.pop();
for(int i=0;i<G[x].size();i++)
{
int y=G[x][i];
if(--in[y]==0)
{
num++;
q.push(y);
}
}

}
int t=n;
for(int i=0;i<n;i++)
{
if(find(i)!=i) t--;
}
if(num<t) conflict=1;

}
int main()
{
int x,y;char op;
while(~scanf("%d%d",&n,&m))
{
int cnt=0;
init();
for(int i=0;i<m;i++)
{
scanf("%d %c %d",&x,&op,&y);
if(op=='=') merge(x,y);
else
{
q[cnt].x=x;
q[cnt].y=y;
q[cnt++].op=op;
}
}
for(int i=0;i<cnt;i++)
{
if(conflict ) continue;
int x=q[i].x;
int y=q[i].y;
int op=q[i].op;
int rootx=find(x);
int rooty=find(y);
if(rootx==rooty)
{
conflict=1;
continue;
}
if(op=='>')add_edge(rootx,rooty);
else add_edge(rooty,rootx);

}
if(!conflict) topsort();

if(conflict) puts("CONFLICT");
else if(uncertain) puts("UNCERTAIN");
else puts("OK");

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: