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);
        }
    }
  • 相关阅读:
    【转】你可能不知道的Shell
    【转】28个Unix/Linux的命令行神器
    ubuntu创建桌面快捷方式
    linux解压zip乱码解决方案
    全能系统监控工具dstat
    【转】linux sar命令详解
    等级保护测评策略建议整改措施
    oracle dataguard详解和环境搭建
    Oracle RMAN备份和异机恢复
    Oracle异机恢复处理
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/11135078.html
Copyright © 2011-2022 走看看