【HDU 1811 Rank of Tetris】(拓扑排序判环 + 并查集合并)

27 篇文章 0 订阅
19 篇文章 0 订阅

自从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

分析:
判断能否确定高手榜,即第一名不能并列。
判断是否冲突,即 存在环就冲突。
并查集 用来合并名次并列的人, 然后拓扑判环
需先合并并列的,然后找根,构建图

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <map>
#include <set>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <stack>
#include <ctime>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;

#define mem(a,n) memset(a,n,sizeof(a))
#define memc(a,b) memcpy(a,b,sizeof(b))
#define rep(i,a,n) for(int i=a;i<n;i++)
#define pb push_back
#define IO ios::sync_with_stdio(false)
#define fre freopen("in.txt","r",stdin)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
const double PI=acos(-1.0);
const double eps=1e-8;
const int INF=0x3f3f3f3f;
const int MOD=1e8;
const int N=1e4+5;
const ll maxn=1LL<<60;
const int dir[4][2]= {-1,0,1,0,0,-1,0,1};
int n,m,sum;
vector<int>g[N];
string s;
int deg[N],fa[N];
struct Node
{
    int u,v;
    char ch;
} a[N];
void init()
{
    rep(i,0,n+1) fa[i]=i;
    rep(i,0,n+1) g[i].clear();
    mem(deg,0);
    mem(a,0);
}
int Find(int x)
{
    return fa[x]==x?fa[x]:Find(fa[x]);
}
bool unite(int x,int y)
{
    x=Find(x),y=Find(y);
    if(x!=y)
    {
        fa[y]=x;
        return 1;
    }
    return 0;
}
bool toposort()
{
    queue<int>que;
    bool flag=1;
    rep(i,0,n) if(!deg[i]&&fa[i]==i) que.push(i);///满足入度为0并且没有父节点的  是根节点
    while(!que.empty())
    {
        if(que.size()>1) flag=0;///根结点数目>1  说明不能确定排名
        int u=que.front();
        que.pop();
        sum--;
        rep(i,0,g[u].size())
        {
            int v=g[u][i];
            if(!(--deg[v])) que.push(v);
        }
    }
    return flag;
}
int main()
{
    ///fre;
    IO;
    while(cin>>n>>m)
    {
        init();
        sum=n;
        rep(i,0,m)
        {
            cin>>a[i].u>>a[i].ch>>a[i].v;
            if(a[i].ch=='='&&unite(a[i].u,a[i].v)) sum--;
           // printf(" sum=%d\n",sum);
        }
        rep(i,0,m)
        {
            if(a[i].ch=='=') continue;
            int u=Find(a[i].u),v=Find(a[i].v);
            if(a[i].ch=='>') g[u].pb(v),deg[v]++;
            else g[v].pb(u),deg[u]++;
        }
        bool flag=toposort();
        if(sum>0) cout<<"CONFLICT\n";///sum>0 说明存在环
        else if(flag) cout<<"OK\n";
        else cout<<"UNCERTAIN\n";
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值