The Unique MST
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!
Source
POJ Monthly--2004.06.27 srbga@POJ
题目类型:判断MST唯一性
算法分析:对构造好的图进行一次MST,然后依次去除MST中权值相同的边,再进行一次MST,如果两次最小权值和相同,则MST不唯一
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
#include <iostream> #include <fstream> #include <algorithm> #include <iomanip> #include <cstring> #include <cstdio> #include <cmath> #include <map> #include <string> #include <vector> #include <stack> #include <queue> #include <set> #include <list> #include <ctime> using namespace std; const int maxn = 166 + 66; struct Node { int u, v, w; bool eqal, used, del; }; Node edge[maxn*maxn]; int parent[maxn], len; int n, m; bool is_first; int UnFind (int val) { if (parent[val] == val) return val; return parent[val] = UnFind (parent[val]); } bool Cmp (Node a, Node b) { return a.w < b.w; } int kruskal () { int sum = 0, remain = n; int i; for (i = 0; i < maxn; i++) parent[i] = i; for (i = 0; i < m && remain > 1; i++) { if (edge[i].del) continue; if (UnFind (edge[i].u) != UnFind (edge[i].v)) { parent[UnFind(edge[i].u)] = UnFind (edge[i].v); sum += edge[i].w; if (is_first) edge[i].used = true; remain--; } } return sum; } int main() { // ifstream cin ("aaa.txt"); int t; cin >> t; while (t--) { cin >> n >> m; int i; for (i = 0; i < m; i++) { int u, v, w; cin >> u >> v >> w; edge[i].u = u - 1; edge[i].v = v - 1; edge[i].w = w; edge[i].eqal = false; edge[i].used = false; edge[i].del = false; } for (i = 0; i < m; i++) { int j; for (j = 0; j < m; j++) { if (i == j) continue; else if (edge[i].w == edge[j].w) edge[i].eqal = true; } } sort (edge, edge + m, Cmp); is_first = true; int val_first = kruskal (), val_second; is_first = false; for (i = 0; i < m; i++) { if (edge[i].used && edge[i].eqal) { edge[i].del = true; val_second = kruskal (); if (val_first == val_second) { cout << "Not Unique!" << endl; break; } edge[i].del = false; } } if (i == m) cout << val_first << endl; } return 0; } |
- « 上一篇:poj1659
- poj1700:下一篇 »