Poj 1679 The Unique MST (minor Spanning Tree)

Source: Internet
Author: User

Poj 1679 The Unique MST (minor Spanning Tree)
The Unique MST

Time Limit:1000 MS Memory Limit:10000 K
Total Submissions:20293 Accepted:7124

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

After reading me for a long time, I still felt a little bad. After reading a lot of blogs from the great gods, I wrote it myself. Finally, the idea was not clear, the idea of dp used in the prim algorithm is not very familiar with dp. In the next stage, we should take a good look at dp. We still need to do more things if the secondary generation tree of krusal has not been fully understood;

Refer to blog http://blog.csdn.net/jarily/article/details/8912538

I just transferred the algorithm introduction to it, but I still need to read it several times.

  1. Algorithm introduction:
  2. * If G = (V, E, w) is a connected undirected graph, T is a minimum spanning tree of graph G;
  3. * If another tree T1 exists, t', ω (t') will not exist ') <ω(t1),则称t1是图g的次小生成树;
  4. *
  5. * Algorithm idea:
  6. * Concept of adjacent set: A set composed of a new spanning tree generated by T for a feasible exchange. It is called the adjacent set of tree T and is recorded as N (T );
  7. * Set T to the Minimum Spanning Tree of graph G. If T1 meets ω (T1) = min {ω (t') | t'ε N (T )}, then T1 is a small generation tree of G;
  8. * First, the minimum spanning tree T and time complexity O (Vlog2V + E) of the graph are obtained );
  9. * Then, calculate the weights of the neighboring sets of T and the smallest spanning tree, that is, the secondary Spanning Tree of graph G;
  10. * Simple enumeration is highly complex;
  11. * The complexity of enumerating the two sides is O (VE), and then determining whether the switching is feasible is O (V). The total time complexity is O (V2E );
  12. * The analysis shows that each edge added to a non-tree can always form a ring. only one side of the ring can be deleted to ensure that the tree is still generated after the switch;
  13. * The larger the value of the deleted edge, the smaller the value and smaller the new spanning tree. This reduces the complexity to O (VE );
  14. * Better method: first, perform a step-by-step preprocessing to find the edge with the maximum weight value on the path between each two nodes in the tree;
  15. * In the enumeration graph, the edge that is not on the tree is obtained. With preprocessing, the edge with the largest weight on the ring can be obtained at O (1) time;
  16. * Preprocessing: as a tree, simply use BFS. the time complexity required for preprocessing is O (V2 );The following code is written using the prim algorithm:

    Here, a path array is used to store the path with the largest weight in two points;

    The low array stores the edge weight;

    Use the pre array to save the position of the precursor, and use a uesd array to mark whether to generate a tree;

    # Include
       
        
    # Include
        
         
    # Define max (a, B) (a)> (B )? (A) :( B) # define min (a, B) (a) <(B )? (A) :( B) const int maxn = 100 + 5; const int INF = 0x3fffffff; int map [maxn] [maxn], low [maxn]; int path [maxn] [maxn], pre [maxn]; // int visit [maxn], used [maxn] [maxn]; // mark the array int n, m; int prim () {int I, j, pos, mst = 0; memset (visit, 0, sizeof (visit); // initialize memset (path, 0, sizeof (path); memset (used, 0, sizeof (used); visit [1] = 1; for (I = 1; I <= n; I ++) {low [I] = map [1] [I]; pre [I] = 1; // record precursor} for (I = 1; I
         
          
    Map [pos] [j]) {low [j] = map [pos] [j]; // update the low array pre [j] = pos ;}}} return mst;} int main () {int t, x, y, w, I, j; scanf ("% d", & t); while (t --) {scanf ("% d", & n, & m); for (I = 1; I <= n; I ++) for (j = 1; j <= n; j ++) map [I] [j] = INF; // initialize the map as INF for (I = 1; I <= m; I ++) {scanf ("% d", & x, & y, & w ); map [x] [y] = map [y] [x] = w;} int mst; int res = INF; mst = prim (); for (I = 1; I <= n; I ++) for (j = 1; j <= n; j ++) {if (I! = J &&! Used [I] [j]) res = min (res, mst + map [I] [j]-path [I] [j]); // generate small Spanning Tree} if (res = mst) puts ("Not Unique! "); Else printf (" % d \ n ", mst);} return 0 ;}
         
        
       
    If the generated child spanning tree is equal to the minimum spanning tree, the Child spanning tree is not unique. Otherwise, the Child spanning tree is unique;

    Add an edge to the minimum spanning tree, and then delete an edge from the minimum spanning tree to obtain the secondary spanning tree. In this example, preprocessing is performed to pre-process paths with large weights.

    This blog's second generation of tree speak of good http://www.cnblogs.com/hxsyl/p/3290832.html

    Kruskal to be updated;

Related Article

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.