zoukankan      html  css  js  c++  java
  • The Unique MST(最小生成树的唯一性判断)

    Given a connected undirected graph, tell if its minimum spanning tree is unique. 

    Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V', E'), with the following properties: 
    1. V' = V. 
    2. T is connected and acyclic. 

    Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E') of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E'. 

    Input

    The first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the following m lines contains a triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.

    Output

    For each input, if the MST is unique, print the total cost of it, or otherwise print the string 'Not Unique!'.

    Sample Input

    2
    3 3
    1 2 1
    2 3 2
    3 1 3
    4 4
    1 2 2
    2 3 2
    3 4 2
    4 1 2
    

    Sample Output

    3
    Not Unique!
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<set>
    #include<vector>
    #include<map>
    #include<cmath>
    const int maxn=1e5+5;
    typedef long long ll;
    using namespace std;
    int pre[maxn],n,m,first;
     
    struct node
    {
        int x,y,val;
        int u;
        int e;
        int d;
    } p[maxn];
    int find(int x)
    {
        if(pre[x]==x)
        {
            return x;
        }
        else
        {
            return pre[x]=find(pre[x]);
        }
    }
    int prime()
    {
        int i,j,k,sum,num;
        sum=0;num=0;
        for(i=1;i<=n;i++) 
        pre[i]=i;
        for(i=1;i<=m;i++) {
            if(p[i].d) continue;
            int fx=find(p[i].x);
            int fy=find(p[i].y);
            if(fx!=fy) {
                num++;
                pre[fx]=fy;
                sum+=p[i].val;
                if(first) 
                p[i].u=1;
            }
            if(num==n-1) break;
        }
        return sum;
    }
    bool cmp(node x,node y)
    {
        if(x.val<y.val)
        return true;
        else
        return false;
    }
    int main()
    {
        int k,u,v,w,sum1,sum2;
        int T;
        scanf("%d",&T);
        while(T--) 
        {
            sum1=sum2=0;
            memset(p,0,sizeof(p));
            scanf("%d%d",&n,&m);
            for(int i=1;i<=m;i++) 
            {
                scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].val);
            }
            for(int i=1;i<=m;i++) {
                for(int j=i+1;j<=m;j++) 
                {
                    if(p[i].val==p[j].val) p[i].e=1;
                }
            }
            sort(p+1,p+1+m,cmp);
            first=1;
            sum1=prime();
            first=0;
            bool flag=false;
            for(int i=1;i<=m;i++) 
            {
                if(p[i].u && p[i].e) 
                {
                    
                    p[i].d=1;
                    sum2=prime();
                    if(sum1==sum2) 
                    {
                        flag=true;
                        printf("Not Unique!
    ");
                        break;
                    }
                }
            }
            if(!flag) 
            printf("%d
    ",sum1);
        }
    }
  • 相关阅读:
    《SQL初学者指南》——第1章 关系型数据库和SQL
    《SQL初学者指南》——第1章 关系型数据库和SQL
    快讯:Oracle自治事务处理数据库发布和19c路线图
    NoReverseMatch: u'polls' is not a registered namespace
    Tomcat配置图片保存路径,图片不保存在项目路径下
    请慎用java的File#renameTo(File)方法
    sentinel monitor mymaster 10.10.17.200 6379 1 1个哨兵同意就切换
    静默错误:Oracle 数据库是如何应对和处理的 ?
    如何对Node.js默认下载路径进行修改
    “我卖一个群可以赚2万,但这个项目,死了!”
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/11135078.html
Copyright © 2011-2022 走看看