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
23 31 2 12 3 23 1 34 41 2 22 3 23 4 24 1 2
Sample output
3Not Unique!
Source
Poj monthly -- 2004.06.27 [email protected]
It is to judge whether the minimum spanning tree is unique. In other words, it means whether an edge of the minimum spanning tree can be replaced by other edges.
Idea: first, let's go through the Minimum Spanning Tree, add the edge marker of the Minimum Spanning Tree, and then remove the edge one by one to see if there is any edge that can replace this edge, that is, the value of the minimum spanning tree remains unchanged.
That's all. You should be clear about it. Check the code.
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace STD; # define n 10005 struct stud {int A, B, Len ;} f [N]; int father [N], vis [N], n, m; int sum; int CMP (stud a, stud B) {return. len <B. len;} int Cha (int x) {If (X! = Father [x]) Father [x] = CHA (father [x]); Return father [X];} int FDD (int x) {int I; for (I = 0; I <= N; I ++) Father [I] = I; int num = 1, ANS = 0; for (I = 0; I <m; I ++) {if (I = x) continue; // The edge of the flag is not added to the Spanning Tree int AA = CHA (F [I]. a); int BB = CHA (F [I]. b); If (Aa! = Bb) {FATHER [AA] = BB; num ++; ans + = f [I]. len; If (num = N) break;} If (num = N & Ans = sum) return 1; // remember that there must be two conditions, cannot return 0;} int main () {int T, I; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M); for (I = 0; I <m; I ++) scanf ("% d", & F [I]. a, & F [I]. b, & F [I]. len); for (I = 0; I <= N; I ++) Father [I] = I; sort (F, F + M, CMP ); // sort int num = 1 by edge; memset (VIS, 0, sizeof (VIS); sum = 0; for (I = 0; I <m; I ++) {int AA = CHA (F [I]. a); int BB = Cha (F [I]. B); If (Aa! = Bb) {FATHER [AA] = BB; num ++; vis [I] = 1; // tag the edge that is added to the Spanning Tree sum + = f [I]. len; // The Minimum Spanning Tree value is sum if (num = N) break; // n vertices, n-1 edges required (Num initial value is 1 )}} int flag = 0; for (I = 0; I <m; I ++) if (vis [I]) // if this edge is added to the spanning tree, let's see if any edge can replace it if (FDD (I) // if it can break; if (I! = M) printf ("not unique! \ N "); else printf (" % d \ n ", sum) ;}return 0 ;}