poj 1679 The Unique MST

时间:2022-12-13 20:44:05

题目连接

http://poj.org/problem?id=1679

The Unique MST

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

1
6 7
1 3 1
1 2 2
2 3 3
3 4 0
4 6 5
4 5 4
5 6 6
9
1 0
4 5
1 2 1
2 3 1
3 4 1
1 4 2
2 4 1
10 15
2 10 97
2 6 18
7 1 63
5 4 62
7 5 93
1 3 10
6 9 99
3 7 73
2 7 6
5 9 22
5 3 82
4 2 36
8 1 50
10 3 20
7 9 69
10 15
10 5 79
4 2 33
4 8 41
9 3 97
5 2 25
2 6 9
2 10 66
8 3 38
10 8 89
1 10 83
1 7 91
7 3 94
7 10 40
7 2 70
2 3 82
10 15
3 8 84
7 10 34
1 10 14
1 9 60
7 6 49
8 5 39
4 5 96
4 7 78
7 3 33
2 8 56
8 9 71
5 2 83
3 6 61
7 9 63
2 6 43
10 15
1 10 25
1 3 14
10 5 72
8 3 18
2 5 41
4 9 86
6 8 17
6 2 98
5 6 34
1 8 90
7 1 65
7 2 63
8 7 71
4 2 64
9 6 50
10 15
2 7 13
5 10 52
5 2 5
10 6 47
9 4 23
8 10 54
1 10 20
4 10 8
6 1 87
8 2 43
8 1 87
6 3 53
3 1 87
2 3 82
4 6 91
10 15
1 2 14
4 1 89
7 6 8
9 4 81
5 2 81
10 9 6
1 5 44
1 3 33
2 6 25
6 10 10
1 10 65
6 9 74
8 10 41
2 3 89
5 10 2
10 15
9 8 14
2 10 66
10 5 73
2 3 98
1 3 30
6 5 3
2 1 84
2 6 33
10 8 24
5 8 34
7 1 69
3 7 60
7 4 38
4 10 65
3 4 32
1
6 7
1 3 1
1 2 2
2 3 3
3 4 0
4 6 5
4 5 4
5 6 6
0

Sample Output

Not Unique!
287
432
406
326
264
220
273

判断最小生成树是否唯一。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
using std::map;
using std::min;
using std::sort;
using std::pair;
using std::vector;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 110;
const int INF = 0x3f3f3f3f;
int V, E;
struct edge {
int u, v, w;
inline bool operator<(const edge &x) const {
return w < x.w;
}
}G[(N * N) << 1], X[N * N];
struct Kruskal {
int par[N], rank[N];
inline void init() {
rep(i, V + 1) {
par[i] = i;
rank[i] = 0;
}
}
inline int find(int x) {
while(x != par[x]) {
x = par[x] = par[par[x]];
}
return x;
}
inline bool unite(int x, int y) {
x = find(x), y = find(y);
if(x == y) return false;
if(rank[x] < rank[y]) {
par[x] = y;
} else {
par[y] = x;
rank[x] += rank[x] == rank[y];
}
return true;
}
inline void built() {
int u, v, w;
rep(i, E) {
scanf("%d %d %d", &u, &v, &w);
G[i] = (edge){ u, v, w };
}
}
inline int kruskal_1(int &p) {
init();
int ans = 0;
rep(i, E) {
int u = G[i].u, v = G[i].v;
if(unite(u, v)) {
ans += G[i].w;
X[p++] = (edge){ u, v, G[i].w };
}
}
return ans;
}
inline int kruskal_2(int x, int y) {
init();
int ans = 0;
rep(i, E) {
int u = G[i].u, v = G[i].v;
if(u == x && y == v) continue;
if(unite(u, v)) {
ans += G[i].w;
}
}
return ans;
}
inline void solve() {
built();
sort(G, G + E);
int p = 0, ans = kruskal_1(p);
rep(i, p) {
int ret = kruskal_2(X[i].u, X[i].v);
int t = -1;
for(int j = 1; j <= V; j++) {
if(par[j] == j) t++;
}
if(t) continue;
if(ret == ans) { ans = -1; break; }
}
if(-1 == ans) puts("Not Unique!");
else printf("%d\n", ans);
}
}go;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int t;
scanf("%d", &t);
while(t--) {
scanf("%d %d", &V, &E);
go.solve();
}
return 0;
}