• UESTC 1959 天才钱vs学霸周 最小生成树唯一性判定


    题目:http://www.qscoj.cn/#/problem/show/1959

    要判断最小生成树是否唯一,我们只需要看每次加入最小生成树的边是否还有选择的余地

    在Kruskal最小生成树的算法中,我们每次取最小权值加入,那么

    cnt1表示在权值相同时可以加入最小生成树的边数

    cnt2表示在权值相同时被加入最小生成树的边数

    如果cnt1>cnt2那么不唯一

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<map>
    #include<set>
    using namespace std;
    struct p
    {
        int x,y;
        long long z;
        bool operator <(p t) const
        {
            return z<t.z;
        }
    };
    p a[200005];
    int f[2005];
    int found(int x)
    {
        return f[x]==x?x:f[x]=found(f[x]);
    }
    int n,m;
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            for(int i=1;i<=m;i++)
                scanf("%d%d%lld",&a[i].x,&a[i].y,&a[i].z);
            sort(a+1,a+1+m);
            for(int i=1;i<=n;i++)
                f[i]=i;
            int cnt1=0,cnt2=0;
            for(int i=1;i<=m;i++)
            {
                for(int j=i;j<=m;j++)
                    if (a[i].z==a[j].z)
                    {
                        int x=found(a[j].x);
                        int y=found(a[j].y);
                        if (x!=y)
                            cnt1++;
                    }
                    else break;
                for(int j=i;j<=m;j++)
                    if (a[i].z==a[j].z)
                    {
                        int x=found(a[j].x);
                        int y=found(a[j].y);
                        if (x!=y)
                        {
                            cnt2++;
                            f[x]=y;
                        }
                    }
                    else break;
            }
            if (cnt1>cnt2) printf("zin
    ");
            else printf("ogisosetsuna
    ");
        }
        return 0;
    }
    

      

  • 相关阅读:
    springboot02-SpringSecurity
    java基础07- 二维数组
    java基础06-数组的使用
    逆向工程核心原理——第四章
    逆向工程核心原理——第三章
    Crackme_1_Acid_burn
    Crackme_2_Afkayas.1
    逆向工程核心原理——第二章
    MessageBox
    Python(基础)
  • 原文地址:https://www.cnblogs.com/bk-201/p/9305957.html
Copyright © 2020-2023  润新知