The Unique MST

 POJ - 1679 

 

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 <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
const int N = 109;
const int MAX = 100000000;
int dis[N];
int g[N][N];
int pre[N];
pair<int, int> v[N];
int prim(int n, int m, bool f)
{
    for(int i=0;i<n;i++)
    {
        dis[i] = g[0][i];
        pre[i] = 0;
    }
    dis[0] = -1;
    int ans = 0;
    for(int i=1;i<n;i++)
    {
        int min = MAX, mini = -1;
 
        for(int j=0;j<n;j++)
        {
            if(dis[j]!=-1 && dis[j]<min)
            {
                min = dis[j];
                mini = j;
            }
        }
        if(mini == -1)
            return -1;
        dis[mini] = -1;
        if(f)
        {
            v[i].first = mini;
            v[i].second = pre[mini];
        }
        ans += min;
        for(int j=0;j<n;j++)
            if(dis[j]!=-1 && dis[j] > g[mini][j])
            {
                pre[j] = mini;
                dis[j] = g[mini][j];
            }
    }
    return ans;
}
int main()
{
    int T;
    IOS;cin>>T;
    while(T--)
    {
        int n, m;
        memset(g, 0x3f, sizeof(g));
        cin >>n >>m;
        int a, b, c;
        for(int i=0;i<m;i++)
        {
            cin >>a >>b >>c;
            g[a-1][b-1] = g[b-1][a-1] = c;
        }
        int ans = prim(n, m, 1);
        for(int i=1;i<n;i++)
        {
            int t = g[v[i].first][v[i].second];
            g[v[i].first][v[i].second]= g[v[i].second][v[i].first] = MAX;
            if(ans == prim(n, m, 0))
            {
                ans = -1;
                break;
            }
            g[v[i].first][v[i].second]= g[v[i].second][v[i].first] = t;
        }
        if(ans == -1)
            cout<<"Not Unique!\n";
        else
            cout<<ans<<endl;
    }
    return 0;
}