POJ-1679 The Unique MST (Ci small spanning tree, determining whether the unique minimum spanning tree)

 

http://poj.org/problem?id=1679

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

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!

 

Meaning of the questions:

M to n point side without heavy side, minimum spanning tree is unique, if the minimum spanning tree is then output only the minimum spanning tree on the right and not only outputs Not Unique!

Ideas:

Seek times spanning small, and if the results are the same as the minimum spanning tree is not unique.

 

The second smallest Spanning Tree:

Spanning from the second smallest minimum spanning tree change comes through the minimum spanning tree concept can know that "small time" requires only a change in the minimum spanning tree on the edge achieved through, and to make this change is minimal.

Gives a very good blog wrote: https://blog.csdn.net/qq_27437781/article/details/70821413

 


 

from:https://blog.csdn.net/u011721440/article/details/38735547

Determine the minimum spanning tree is unique:

1, FIG every edge, scanning the other side, if the same edge weight, the edge is marked.

2, MST is obtained by kruskal or prim.

3, if the unmarked side of the MST, MST is unique; otherwise, in order to remove the MST mark edge then seek MST, MST is obtained when the same weight and the original weight MST, MST is not unique.


from:https://blog.csdn.net/blue_skyrim/article/details/51338375

The method of seeking the next smallest spanning enumerate each edge of the minimum spanning tree, wherein the one side is removed, the other side to find these two points, the remaining edges form the minimum spanning tree


 

 

kuangbin Gangster blog: https://www.cnblogs.com/kuangbin/p/3147329.html

Ideas:

When the minimum spanning tree, an array MAXVAL [i] [j] to the MST i to j represent the maximum right side, after seeking, not directly enumerate all sides of the MST, replace the right side edge of the maximum, the answer to update , pay attention to point number from zero

principle:

Not adjacent to two points on the minimum spanning tree must be connected to become a ring, so we can not attempt to enumerate these adjacent points so that they are connected, and then delete the maximum side ring belonging to the minimum spanning tree (let the current is determined the point u, which has been determined as the point v, then u - v path from either side with the greatest v - pre [u] is the maximum path, or is currently determined edge lowval [u], dp thinking), it will ensure the tree structure and make the smallest change in the tree. The enumeration in the minimum spanning tree result is the second smallest.

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <iostream>
  4 #include <string>
  5 #include <math.h>
  6 #include <algorithm>
  7 #include <vector>
  8 #include <stack>
  9 #include <queue>
 10 #include <set>
 11 #include <map>
 12 #include <sstream>
 13 const int INF=0x3f3f3f3f;
 14 typedef long long LL;
15  const  int MOD = + 1E9 . 7 ;
 16  // const Double the PI = ACOS (-1); 
. 17  #define the Bug COUT << "--------------------- "<< endl
 18 is  const  int MAXN = 110 ;
 . 19  the using  namespace STD;
 20 is  
21 is  int G [MAXN] [MAXN]; // adjacency matrix 
22 is  int VIS [MAXN]; // determines there is no point in the minimum spanning tree 
23  int pre [MAXN]; // parent each point 
24  int lowval [MAXN]; // auxiliary array 
25  intMAXVAL [MAXN] [MAXN]; // MAXVAL [i] [j] denotes the largest edge in the minimum spanning tree weight from i to j path 
26 is  int Used [MAXN] [MAXN]; // Analyzing this edge whether used in a minimum spanning tree 
27  int the MST; // minimum spanning tree weight and 
28  
29  int Prim ( int n-, int ST) // n-number of vertices, st is the starting vertex of the minimum spanning tree 
30  {
 31 is      Fill (lowval, lowval + n-, INF);
 32      Memset (MAXVAL, 0 , the sizeof (MAXVAL));
 33 is      Memset (pre, - . 1 , the sizeof (pre));
 34 is      Memset (Used,0,sizeof(used));
 35     memset(vis,0,sizeof(vis));
 36     int ans=0;
 37     lowval[st]=0;
 38     vis[st]=1;
 39     for(int i=0;i<n;i++)
 40     {
 41         if(i!=st&&G[st][i]!=INF)
 42         {
 43             lowval[i]=min(lowval[i],G[st][i]);
 44             pre[i]=st;
 45         }
 46     }
 47     for(int k=0;k<n-1;k++)
 48     {
 49         int MIN=INF;
 50         int t=-1;
 51         for(int i=0;i<n;i++)
 52         {
 53             if(vis[i]==0&&lowval[i]<MIN)
 54             {
 55                 MIN=lowval[i];
 56                 t=i;
57 is              }
 58          }
 59  //         IF (MIN == INF) return -1; 
60          ANS + = MIN;
 61 is          VIS [T] = . 1 ;
 62 is          Used [T] [pre [T]] = Used [pre [T]] [T] = . 1 ; // tag this edge in the minimum spanning tree 
63 is          for ( int I = 0 ; I <n-; I ++ )
 64          {
 65              IF (VIS [I]) 
 66                  MAXVAL [T] [I] = MAXVAL [I] [T] = max (MAXVAL [I] [pre [T]], lowval [T]);
 67              IF !! (I = T && VIS [I] && G [T] [I] <lowval[i])
 68             {
 69                 pre[i]=t;
 70                 lowval[i]=G[t][i];
 71             }
 72         }
 73     }
 74     return ans;
 75 }
 76 
 77 int Judge(int n)
 78 {
 79     int MIN=INF;
 80     for(int i=0;i<n;i++)
 81     {
 82         for(int j=i+. 1 ; J <n-; J ++ )
 83          {
 84              IF ! (G [I] [J] = INF && Used [I] [J])! // edge is not minimum spanning tree 
85                  MIN = min (MIN, MST- MAXVAL [I] [J] + G [I] [J]);
 86          }
 87      }
 88      return MIN;
 89  }
 90  
91 is  int main ()
 92  {
 93      int T;
 94      Scanf ( " % D " , & T) ;
 95      the while (T-- )
 96      {
 97          int n,m;
 98         scanf("%d %d",&n,&m);
 99         memset(G,INF,sizeof(G));
100         for(int i=0;i<m;i++)
101         {
102             int u,v,w;
103             scanf("%d %d %d",&u,&v,&w);
104             u--;v--;//使标号从0开始 
105             G[u][v]=w;
106             G[v][u]=w;
107         }
108          the MST = Prim (n-, 0 );
 109          IF (== Judge the MST (n-)) // minimum spanning tree next smallest and spanning a total weight equal to 
110              the printf ( " Not of Unique \ n-! " );
 111          the else 
112              the printf ( " % D \ n- " , the MST);
 113      }
 114      return  0 ;
 115 }

 

Guess you like

Origin www.cnblogs.com/jiamian/p/11735971.html