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

题意:………..(ps:注意“如果信息中同时包含冲突且信息不完全”,所以先要判断信息是否冲突)

思路:明显的拓扑,但多了个”=“号,我们考虑把相等的合并,所以使用并查集预处理数据,再根据并查集得出的块进行建图拓扑。除此之外就是一些细节需要注意,详情看code

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <functional>
#define inf 0X3f3f3f3f
using namespace std;
typedef long long ll;
const int MAXN=1e5+10;
const int MAX=20000+10;
const double eps=1e-6;

int n,m,cnt,tag;
int du[MAX],f[MAX];
int first[MAX],num;
struct NODE{
    int a,b;
    char ch;
}node[MAX];
struct EDGE{
    int v,next;
}edge[MAX];

void init(){
    cnt=0;
    tag=0;
    for(int i=0;i<=n;i++){
        first[i]=-1;
        du[i]=0;
        f[i]=i;
    }
}

void addedge(int u,int v){
    edge[num].v=v;
    edge[num].next=first[u];
    first[u]=num++;
}

int find(int x){
    return (f[x]==x)?f[x]:f[x]=find(f[x]);
}

void merge(int x,int y){
    int xt=find(x);
    int yt=find(y);
    if(xt!=yt)
        f[xt]=yt;
}

void tsort(){
    queue<int>q;
    int flag=0;
    for(int i=1;i<=n;i++){
        int t=find(i);
        if(f[t]==i&&du[t]==0){  //排除已经合并的数据
            q.push(t);
        }
    }
    while(q.size()){
        if(q.size()>1)  flag=1;     //结果不止一个
        int u=q.front();
        q.pop();
        cnt++;
        for(int i=first[u];i!=-1;i=edge[i].next){
            int v=edge[i].v;
            du[v]--;
            if(du[v]==0)    q.push(v);
        }
    }
    if(cnt<n||tag)  cout<<"CONFLICT"<<endl;
    else if(flag)   cout<<"UNCERTAIN"<<endl;
    else    cout<<"OK"<<endl;
}

int main(){
    #ifdef ONLINE_JUDGE
    #else
    freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    #endif
    while(cin>>n>>m){
        init();
        for(int i=0;i<m;i++){
            scanf("%d %c %d",&node[i].a,&node[i].ch,&node[i].b);
            node[i].a++;    //使数据从1开始
            node[i].b++;
            if(node[i].ch=='='){
                merge(node[i].a,node[i].b);         
                cnt++;
            }
        }
        for(int i=0;i<m;i++){
            if(node[i].ch=='=') continue;
            int x=find(node[i].a);
            int y=find(node[i].b);
            if(x==y)    tag=1;   //排除信息错误 如 1=2 2=3 1>3
            if(node[i].ch=='<'){
                addedge(y,x);
                du[x]++;
            }
            else{
                addedge(x,y);
                du[y]++;
            }
        }
        tsort();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值