The Unique MST
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 14402 Accepted: 4981

Description
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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
 
using namespace std;
 
const long long INF = 1 << 31 -1;
int p[11000];
long long mm;
long long tmp;
int n, m;
int fflag;
int del;
 
struct edge{
    int u;
    int v;
    long long w;
    int flag;
}e[10001000];
 
int find(int x){
    return x == p[x] ? x : find(p[x]);
}
 
int cmp(edge a, edge b){
    return a.w < b.w;
}
 
int Kruskal(){
    long long ans = 0;
    int i;
    for(i = 1; i <= n; i ++)
        p[i] = i;
    sort(e + 1, e + m + 1, cmp);
    for(i = 1; i <= m; i ++){
        int x = find(e[i].u);
        int y = find(e[i].v);
        if(x != y){
            e[i].flag = 1;
            ans += e[i].w;
            p[x] = y;
        }
    }
    return ans;
}
 
long long Kruskalm(){
    long long ans = 0;
    int i;
    for(i = 1; i <= n; i ++)
        p[i] = i;
    sort(e + 1, e + m + 1, cmp);
    int j = 0;
    fflag = 0;
    for(i = 1; i <= m; i ++){
        if(i == del)
            continue;
        int x = find(e[i].u);
        int y = find(e[i].v);
        if(x != y){
            ans += e[i].w;
            p[x] = y;
            j ++;
        }
    }
    if(j < n - 1) //n-1
        ans = INF;
    return ans;
}
 
int main(){
    int t, i, tt;
    cin >> t;
    while(t --){
        int ff = 0;
        scanf("%d %d", &n ,&m);
        for(i = 1; i <= m; i ++){
            scanf("%d %d %d", &e[i].u, &e[i].v, &e[i].w);
            e[i].flag = 0;
        }
        mm = Kruskal();
        for(i = 1; i <= m; i ++){
            if(e[i].flag){
                //tt = e[i].w;
                del = i;
                tmp = Kruskalm();
                if(tmp == mm){
                    ff = 1;
                    break;
                }
            }
        }
            if(ff){
                printf("Not Unique!\n");
            }
            else
                printf("%I64d\n", mm);
    }
    return 0;
}