Poj 1679 the unique MST (judge whether the minimum spanning tree is one)

Source: Internet
Author: User
The unique MST
Time limit:1000 ms   Memory limit:10000 K
Total submissions:17406   Accepted:6030

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
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 srbga @ poj

Method for determining whether the minimum spanning tree is one:

1) scan other edges for each edge in the graph. If an edge with the same weight exists, mark the edge;

2) then use the Kruskal algorithm (or prim algorithm) to calculate the MST;

3) after obtaining the MST, if the MST does not contain the marked edge, the MST can be judged to be unique. if it contains the marked edge, remove the marked edge and then obtain the MST, if the obtained MST weight is the same as that of the original MST, the MST is not unique.


Code:

# Include <cstdio> # include <cstring> # include <vector> # include <algorithm> # define maxn 105 using namespace STD; int n, m, num, ans; int set [maxn], rank [maxn]; bool vis [maxn * maxn]; bool eq [maxn * maxn]; struct tnode {int V1, V2, cost ;} node [maxn * maxn]; void Init () {int I; for (I = 1; I <= N; I ++) {set [I] = I; rank [I] = 1;} num = N;} int find (INT X) // optimized path {int r = x, nn; while (set [R]! = R) r = set [R]; while (X! = R) {nn = set [X]; set [x] = r; X = nn;} return r;} void Merge (int A, int B) // when merging, merge small sets of depth into large ones {int X, Y; X = find (a); y = find (B); If (X! = Y) {If (rank [x] <rank [y]) set [x] = y; else {set [y] = X; if (rank [x] = rank [y]) rank [x] ++;} num -- ;}} bool CMP (const tnode & xx1, const tnode & xx2) {return xx1.cost <xx2.cost;} void solve () // record whether each edge has the same weight {int I, j, flag, Ni, nx; memset (EQ, 0, sizeof (EQ); for (I = 1; I <= m; I ++) {flag = 0; ni = I; nx = node [I]. cost; I ++; if (I> m) break; while (node [I]. cost = Nx) {flag = 1; eq [I] = 1; I ++;} If (FLAG) eq [Ni] = 1; I --;} bool Isunique () // determines whether the minimum spanning tree is unique {int I, j, x1, x2, Ni, flag, Tans; flag = 1; for (I = 1; I <= m; I ++) {If (vis [I] & eq [I]) {flag = 0; break ;}} if (FLAG) return true; for (I = 1; I <= m; I ++) {If (vis [I] & eq [I]) // remove each vertex in sequence and then obtain MST {Ni = I; tans = 0; Init (); // remember to initialize for (j = 1 each time; num> 1 & J <= m; j ++) {If (j = ni) continue; X1 = find (node [J]. v1); x2 = find (node [J]. v2); If (x1! = X2) {Merge (node [J]. v1, node [J]. v2); tans + = node [J]. cost ;}}if (TANS = ans) return false ;}return true ;}void Kruskal () {int I, j, x1, x2; ans = 0; memset (VIS, 0, sizeof (VIS); for (I = 1; num> 1 & I <= m; I ++) {x1 = find (node [I]. v1); x2 = find (node [I]. v2); If (x1! = X2) {Merge (node [I]. v1, node [I]. v2); vis [I] = 1; ans + = node [I]. cost ;}}int main () {int I, j, x1, x2, T; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M); Init (); for (I = 1; I <= m; I ++) {scanf ("% d", & node [I]. v1, & node [I]. v2, & node [I]. cost);} Sort (node + 1, node + m + 1, CMP); solve (); Kruskal (); If (isunique ()) printf ("% d \ n", ANS); else printf ("not unique! \ N ") ;}return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.