(拓扑排序)【HDU 1811】Rank of Tetris

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1811

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = 1e5+10;

int u[MAXN],v[MAXN],f[MAXN],son[MAXN];
char c[MAXN];
vector<int> g[MAXN];

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

int Union(int x,int y)
{
    int a = Find(x),b = Find(y);
    if(a == b)  return 0;
    f[a] = b;
    return 1;
}

int main(void)
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(son,0,sizeof son);
        for(int i = 0; i <= n; ++i)
        {
            f[i] = i;
            g[i].clear();
        }
        int num = n;
        for(int i = 0; i < m; ++i)
        {
            scanf("%d %c %d",&u[i],&c[i],&v[i]);
            if(c[i] == '=')
                num -= Union(u[i],v[i]);
        }
        for(int i = 0; i < m; ++i)
        {
            if(c[i] == '=')   continue;
            int x = Find(u[i]), y = Find(v[i]);
            if(c[i] == '>')
            {
                g[x].push_back(y);
                ++son[y];
            }
            else
            {
                g[y].push_back(x);
                ++son[x];
            }
        }
        queue<int>que;
        for(int i = 0; i < n; ++i)
        {
            if(son[i]==0 && i == Find(i))
                que.push(i);
        }
        int only = 1;
        while(!que.empty())
        {
            if(que.size() > 1)    only = 0; //只能有一个入度为0的
            int cur = que.front();
            que.pop();
            --num;
            for(int i = 0; i < g[cur].size(); ++i)
            {
                if(--son[g[cur][i]] == 0)
                    que.push(g[cur][i]);
            }
        }
        if(num > 0)
            puts("CONFLICT");
        else if(!only)
            puts("UNCERTAIN");
        else
            puts("OK");
    }
}

猜你喜欢

转载自blog.csdn.net/suiguia/article/details/81205006