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);
        }
    }
  • 相关阅读:
    2016北京集训测试赛(十四)Problem A: 股神小L
    2016北京集训测试赛(十三) Problem B: 网络战争
    BZOJ 3473 字符串
    2016北京集训测试赛(十一)Problem C: 树链问题
    微信服务号登陆优化
    重新认识下数组的concat方法
    微信服务号开发碰到的缓存问题!
    关于微信服务号开发的一些总结!
    JQ-weui中的日期选择控件关于时间段的设置!
    用json方法来作深拷贝应该知道的一点东西!
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/11135078.html
Copyright © 2011-2022 走看看