HDU-1811 Rank of Tetris

HDU-1811 Rank of Tetris

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.如果是‘UNCERTAIN’说明同时存在入度为0的点(只需要判断队列中是否存在两个及以上入度的点)

2.如果是‘CONFLICT’说明存在环,如果存在环则拓扑排序不完全(只需要判断拓扑排序是否全排序)

3.如果相等并查集合并,如果不想等则建边

4.要注意的是必须先把相等的合并处理后才能处理其他的

#include <iostream>
#include<algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#define endl '
'
#define debug(case,x) cout<<case<<"  : "<<x<<endl
#define INF 0x3f3f3f3f
#define DOF 0x7f7f7f7f
#define mem(a,b) memset(a,b,sizeof(a))
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
typedef long long ll;
using namespace std;

const int maxn = 1e5 + 10;
vector<int>vt[maxn];
int deg[maxn],f[maxn];
queue<int>qt;
int n,m,sum;
struct node{
    int a,b;char ch;
}node[maxn*2];
int Find(int x)
{
    return x==f[x]?x:f[x]=Find(f[x]);
}

void topo()
{
    for(int i=0;i<n;++i){
        if(f[i]==i&&!deg[i]){
            qt.push(i);
            deg[i]--;
        }
    }
    bool flag=false;
    while(!qt.empty()){
        int x=qt.front();qt.pop();
        if(!qt.empty()){
            flag=true;
        }
        for(auto &i:vt[x]){
            --deg[i];
            if(!deg[i]){
                qt.push(i);
                deg[i]--;
            }
        }
        --sum;
    }
    if(sum) puts("CONFLICT");
    else if(flag) puts("UNCERTAIN");
    else puts("OK");
}
int main()
{IO;
    //    freopen("ii.txt","r",stdin);
    //   freopen("oo.txt","w",stdout);
    while(~scanf("%d%d",&n,&m)){
        sum=n;
//        mem(vt,0);mem(deg,0);
        for(int i=0;i<n;++i){
            f[i]=i,deg[i]=0,vt[i].clear();
        }
        while(!qt.empty()) qt.pop();
        for(int i=0;i<m;++i){
            scanf("%d %c %d",&node[i].a,&node[i].ch,&node[i].b);
            if(node[i].ch=='='){
                int fx=Find(node[i].a),fy=Find(node[i].b);
                if(fx!=fy){
                    f[fx]=fy;
                    --sum;
                }
            }
        }
        for(int i=0;i<m;++i){
            if(node[i].ch=='=') continue;
            int fx=node[i].a,fy=node[i].b;
            fx=Find(fx),fy=Find(fy);
            if(node[i].ch=='>'){
                vt[fx].push_back(fy);
                ++deg[fy];
            }else{
                vt[fy].push_back(fx);
                ++deg[fx];
            }
        }
        topo();
    }
}

原文地址:https://www.cnblogs.com/waryan/p/12844498.html