zoukankan      html  css  js  c++  java
  • Rank of Tetris HDU1811 并查集+拓扑排序

    题意:有A>B A=B A<B 三种情况,是否能通过多次判断出各自排名

    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<iostream>
    #include<algorithm>
    #include<string>
    #include<cmath>
    #include<cstring>
    #include<set>
    #include<map>
    #include<queue>
    #include<time.h>
    #include<cstdio>
    #include<stack>
    using namespace std; 
    typedef long long ll;
    const int maxn = 1000000;
    const int mod = 1e9+7;
    #define pb push_back
    // #define long long int
    #define INF 0x3f3f3f3f;
    using namespace std;
    /*
        2020.10.15
    */
    int pre[maxn];
    int mk[maxn];
    int n,m;
    int sum;
    int a[maxn];
    int b[maxn];
    char op[maxn];
    vector<int> nex[maxn];
    
    int find(int x)
    {
        if(x!=pre[x])
            pre[x] = find(pre[x]);
        return pre[x];
    }
    void init()
    {
        for(int i=0;i<n;i++)
        {
            pre[i] = i;
            mk[i] = 0;
            nex[i].clear();
        }
    }
    bool uoin(int x,int y)
    {
        int u = find(x);
        int v = find(y);
        if(u==v)
            return true;
    
        pre[u] = v;
        return false;
    }
    void topsort()
    {
        queue<int> q;
        bool flag = 0;
    
    
        for(int i=0;i<n;i++)
            if( mk[i] == 0 && pre[i] == i)
                q.push(i);
            
        
        while(!q.empty())
        {
            if(q.size()>1) flag = 1;
            int cur = q.front();
    
            q.pop();
            sum--;
    
            for(int i=0;i<nex[cur].size();i++)
            {
                if(--mk[nex[cur][i]]==0)
                {
                    q.push(nex[cur][i]);
                }
            }
        }
        if( sum>0 ) printf("CONFLICT
    ");
        else if(flag) printf("UNCERTAIN
    ");
        else printf("OK
    ");
    }
    int main()
    {
        while(scanf("%d %d",&n,&m)!=EOF)
        {
            init();
            sum = n;
            for(int i=0;i<m;i++)
            {
                scanf("%d %c %d",&a[i],&op[i],&b[i]);
                if(op[i]=='=')
                {
                    if(!uoin(a[i],b[i]))
                    {
                        sum--;
                    }
                }
            }
    
            for(int i=0;i<m;i++)
            {
                if(op[i]=='=')continue;
            
                int u = find(a[i]);
                int v = find(b[i]);
    
                if(op[i]=='>')
                {
                    nex[u].pb(v);
                    mk[v]++;
                }
                else
                {
                    nex[v].pb(u);
                    mk[u]++;
                }
            }
            topsort();
        }
        //system("pause");
    }
    View Code

    题解: A=B时候 并查集uoin 拓扑排序总数 - 1 , A>B A<B 情况进行拓扑排序

    mk[i] 表示该点入度

  • 相关阅读:
    记一次大数据量后台服务的性能优化(转载)
    javascript void(0)
    IE11浏览器设置为兼容不同IE版本
    Asp.net WebForm(4) Server对象、跳转分析、验证控件和站点导航
    http请求错误码(转载)
    windows系统和IE的兼容性问题
    Lombok常用注解
    Typora配置图床PicGo,使用阿里云Oss 存储
    Picgo +sm.ms打造免费图床个人工具
    OSS —— 对象存储介绍
  • 原文地址:https://www.cnblogs.com/lightWh1te/p/13830760.html
Copyright © 2011-2022 走看看