K - The Unique MST (minimum spanning tree of the uniqueness)

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 of Unique 
Solution: First calculate the weights and the minimum spanning tree ans, then delete the minimum spanning tree enumerate each side, if you can also achieve the same effect, it shows the minimum spanning tree is not unique,
because two different the minimum spanning tree differ in at least one side, so that we can enumerate delete each edge.
#include<iostream>
#include<vector>
#include<algorithm>
#include<stdio.h>
using namespace std;
typedef long long ll;
const int maxn=100010;
int f[maxn];
struct node
{
    int u,v,w;
    bool operator < (const node &r)const{
        return w<r.w;
    }
}q[maxn];
int Find(int x)
{
    return f[x]==x?x:f[x]=Find(f[x]);
}
int Merge(int u,int v)
{
    u=Find(u);
    v=Find(v);
    if(u!=v)return f[u]=v,1;
    return 0;
}
vector<int>v;
int main()
{
    int T;
    cin>>T;
    while(T--){
        v.clear();
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++)f[i]=i;
        for(int i=1;i<=m;i++){
            cin>>q[i].u>>q[i].v>>q[i].w;
        }
        sort(q+1,q+1+m);
        int ans=0;
        for(int i=1;i<=m;i++){
            int x=Merge(q[i].u,q[i].v);
            if(x){
                v.push_back(i);
                ans+=q[i].w;
            }
        }
        int flag=1;
        for(int i=0;i<v.size();i++){
            int sum=0,cnt=0;
            for(int j=1;j<=n;j++)f[j]=j;
            for(int j=1;j<=m;j++){
                if(j==v[i])continue;
                int x=Merge(q[j].u,q[j].v);
                if(x){
                    sum+=q[j].w;
                    cnt++;
                }
            }
            if(cnt==n-1&&ans==sum){
                flag=0;
                break;
            }
        }
        if(flag)cout<<ans<<endl;
        else printf("Not Unique!\n"); 

    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/cherish-lin/p/11332866.html