POJ 1679 The Unique MST
The Unique MST
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 37818 | Accepted: 13835 |
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'.
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
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 #include <cctype> 6 7 #define int long long 8 9 using namespace std; 10 11 const int N = 405; 12 13 int cge, T, n, m, x, y, z, ec, sum, gc, ans, 14 fa[N], f[N], use[N << 9], faz[25][N], mx1[25][N], dep[N]; 15 16 struct Edge 17 { 18 int u, v, nxt, w; 19 bool operator < (const Edge &b) const 20 { return w < b.w; } 21 }e[N << 9], g[N << 9]; 22 23 inline void read(int &x) 24 { 25 int k = 1; x = 0; 26 char c = getchar(); 27 while (!isdigit(c)) 28 if (c == '-') k = -1, c = getchar(); 29 else c = getchar(); 30 while (isdigit(c)) 31 x = (x << 1) + (x << 3) + (c ^ 48), 32 c = getchar(); 33 x *= k; 34 } 35 36 inline void Add(int a, int b, int c) 37 { 38 ++gc; 39 g[gc].u = a; 40 g[gc].v = b; 41 g[gc].w = c; 42 } 43 44 inline void Addedge(int a, int b, int c) 45 { 46 ++ec; 47 e[ec].u = a; 48 e[ec].v = b; 49 e[ec].w = c; 50 e[ec].nxt = f[a]; 51 f[a] = ec; 52 } 53 54 inline void Init() 55 { 56 cge = 9223372036854775807, ec = gc = 0, sum = 0, ans = 0; 57 read(n), read(m); 58 memset(f, -1, sizeof(f)); 59 memset(use, 0, sizeof(use)); 60 memset(dep, 0, sizeof(dep)); 61 memset(faz, 0, sizeof(faz)); 62 memset(mx1, 0, sizeof(mx1)); 63 for (int i = 1; i <= n; ++i) fa[i] = i; 64 for (int i = 1; i <= m; ++i) 65 read(x), read(y), read(z), 66 Add(x, y, z); 67 } 68 69 int Find(int x) 70 { 71 return fa[x] == x ? x : fa[x] = Find(fa[x]); 72 } 73 74 inline bool One(int a, int b) 75 { 76 a = Find(a); 77 b = Find(b); 78 return a == b; 79 } 80 81 inline void Union(int a, int b, int u) 82 { 83 int ra = Find(a); 84 int rb = Find(b); 85 ans += g[u].w; ++sum; 86 fa[ra] = rb; use[u] = 1; 87 Addedge(a, b, g[u].w); 88 Addedge(b, a, g[u].w); 89 return; 90 } 91 92 inline void Kruskal() 93 { 94 sort(g + 1, g + 1 + gc); 95 for (int i = 1; i <= gc; ++i) 96 { 97 if (!One(g[i].v, g[i].u)) 98 Union(g[i].u, g[i].v, i); 99 if (sum == n - 1) break; 100 } 101 dep[1] = 1; 102 } 103 104 void DFS(int u) 105 { 106 for (int i = f[u]; i != -1; i = e[i].nxt) 107 { 108 if (e[i].v == faz[0][u]) continue; 109 faz[0][e[i].v] = u, 110 mx1[0][e[i].v] = e[i].w, 111 dep[e[i].v] = dep[u] + 1, 112 DFS(e[i].v); 113 } 114 } 115 116 inline void Make() 117 { 118 for (int j = 1; j <= 20; ++j) 119 for (int i = 1; i <= n; ++i) 120 faz[j][i] = faz[j - 1][faz[j - 1][i]], 121 mx1[j][i] = max(mx1[j - 1][i], mx1[j - 1][faz[j - 1][i]]); 122 } 123 124 inline void LCA(int a, int b, int c) 125 { 126 int m1 = 0, m2 = 0; 127 if (dep[a] < dep[b]) swap(a, b); 128 for (int i = 20; i >= 0; --i) 129 if (dep[faz[i][a]] >= dep[b]) 130 m1 = max(m1, mx1[i][a]); 131 if (a == b) { cge = min(cge, c - m1); return; } 132 for (int i = 20; i >= 0; --i) 133 if (faz[i][b] != faz[i][a]) 134 m1 = max(m1, max(mx1[i][a], mx1[i][b])); 135 m1 = max(m1, max(mx1[0][a], mx1[0][b])); 136 cge = min(cge, c - m1); 137 } 138 139 inline void SecMST() 140 { 141 for (int i = 1; i <= m; ++i) 142 if (!use[i]) 143 LCA(g[i].u, g[i].v, g[i].w); 144 if (cge) printf("%lld\n", ans); 145 else puts("Not Unique!"); 146 } 147 148 signed main() 149 { 150 read(T); 151 while (T--) Init(), Kruskal(), DFS(1), Make(), SecMST(); 152 return 0; 153 }