Poj 1679 The Unique MST, secondary Spanning Tree, pojmst
Secondary Generation tree
When the Minimum Spanning Tree is obtained, the array Max [I] [j] is used to represent the maximum edge weight from I to j in the MST.
After the result is obtained, enumerate all edges that are not in the MST, replace the edges with the maximum edge weight, and update the answer.
# Include <cstdio> # include <cstring> # include <algorithm> using namespace std; const int maxn = 110; const int INF = 1e9; bool vis [maxn]; int d [maxn]; int pre [maxn]; int Max [maxn] [maxn]; bool used [maxn] [maxn]; int g [maxn] [maxn]; int n, m; int Prim () {int ans = 0; memset (vis, false, sizeof vis); memset (Max, 0, sizeof Max); memset (used, false, sizeof used); vis [0] = true; pre [0] =-1; for (int I = 1; I <n; ++ I ){ D [I] = g [0] [I]; pre [I] = 0 ;}for (int I = 1; I <n; ++ I) {int p =-1; for (int j = 0; j <n; ++ j) if (! Vis [j] & (p =-1 | d [p]> d [j]) p = j; if (-1 = p) return-1; ans + = d [p]; vis [p] = true; used [p] [pre [p] = used [pre [p] [p] = true; for (int j = 0; j <n; ++ j) {if (vis [j]) Max [j] [p] = Max [p] [j] = max (Max [j] [pre [p], d [p]); if (! Vis [j] & d [j]> g [p] [j]) {d [j] = g [p] [j]; pre [j] = p ;}}return ans;} void solve () {int res = Prim (); if (-1 = res) {// the graph is Not connected to puts ("Not Unique! "); Return;} int Mn = INF; for (int I = 0; I <n; ++ I) for (int j = I + 1; j <n; ++ j) if (g [I] [j]! = INF &&! Used [I] [j]) {Mn = min (Mn, g [I] [j]-Max [I] [j]);} if (Mn = INF | Mn! = 0) {// The Child tree does Not exist or is Not equal to printf ("% d \ n", res);} else {puts ("Not Unique! ") ;}} Int main () {int T; scanf (" % d ", & T); while (T --) {scanf (" % d ", & n, & m); for (int I = 0; I <n; ++ I) for (int j = 0; j <n; ++ j) g [I] [j] = INF; int x, y, z; while (m --) {scanf ("% d", & x, & y, & z); x --; y --; g [x] [y] = g [y] [x] = z;} solve ();} return 0 ;}
Is the minimum spanning tree the only answer?
Abstract: The minimal spanning tree is a classic problem in graph theory. It is important to find the Minimum Spanning Tree and the weights of the Minimum Spanning Tree. For a given graph, because the weights and values of the Minimum Spanning Tree are fixed, the minimum spanning tree is not unique only when the shape of the Minimum Spanning Tree is not unique. In this paper, we propose three methods for judgment and give them an analysis and evaluation. Key words: min spanning tree; unique; prim algorithm; kruskal algorithm; Sub-small Spanning Tree Classification: TP301.6 Document Identification Code: article A No.: 1007-9599 (2011) 06--02minimum Spanning Tree If the Unique Wu Yuliang, Kong Fanlong (Central China Normal University, Wuhan430079, China) Abstract: Minimum spanning tree is a classic problem of graph theory, find the minimum spanning tree, and find the weight and get enough attention, and few people to study the minimum sp Anning tree is unique. for a given graph is concerned, because the weights and the minimum spanning tree is determined, so the minimum spanning tree is not unique and only if the shape of the minimum spanning tree is not unique. determine whether the proposed minimum spanning tree, and the only three ways to give their analysis and evaluation. keywords: Minimum spanning tree; Unique; Prim algorithm; Kruskal Algorithm; Small spanning tree one or three methods are used to determine whether the minimum spanning tree (MST) is unique (1) the basic idea of the prim algorithm proposed by using the prim algorithm is: first, select any vertex v in the graph as the root of the tree and add it to the set Q of the Spanning Tree. Then, the vertex w is continuously generated into the tree (in set Q, vertex w satisfies the fact that there is an edge between a vertex in the Set Q, and the weight on the edge is the minimum weight among all nodes connected to the set Q and the edges not in the Set Q, after n-1 nodes are added, MST is formed. (3202 words remaining)