The Unique MST(最小生成树的唯一性判断)

回眸只為那壹抹淺笑 提交于 2020-01-18 03:01:00
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!\n");
                    break;
                }
            }
        }
        if(!flag) 
        printf("%d\n",sum1);
    }
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!