Poj1679the Unique MST (sub-niche into a tree)

Source: Internet
Author: User

The Unique MST
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 25203 Accepted: 8995

Description

Given a connected undirected graph, tell if it minimum spanning tree is unique.

Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of 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, which has the and 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 <=), the number of test cases. Each case represents a graph. It begins with a line containing, integers n and m (1 <= n <=), the number of nodes and edges. Each of the following m lines contains a triple (xi, Yi, WI), indicating that Xi and Yi is connected by an edge with Weig HT = wi. For any of the nodes, there is at the most one edge connecting them.

Output

For each input, if the MST was 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!

Test instructions: Ask if a minimum spanning tree is unique
Find a niche into a tree, if equality is not unique, otherwise the only

Sub-niche into a tree: is the smallest spanning tree to a side of the spanning tree; use Maxd "x" "Y" to store the weight of the largest edge in the two-node (x, y) path of the smallest spanning tree, which is the largest of the X-z-...-y in the smallest spanning tree, and then you can change the edges. In the end, which side will never be in the spanning tree edge an enumeration, assuming that X-y, if you want to replace x-z...-y with X-y will definitely have to replace the x-z...-y of the maximum weight of the edge to make the final result is only smaller than x-z...-y, that is, after
1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5 using namespacestd;6 Const intMAX = the;7 Const intINF =100000000;8 intn,m,ans,cnt;9 intEdge[max][max],vis[max],used[max][max],dist[max];Ten intPre[max],maxd[max][max]; One voidPrime () A { -memset (Vis,0,sizeof(Vis)); -memset (Used,false,sizeof(used)); thememset (Maxd,0,sizeof(Maxd)); -      for(inti =1; I <= N; i++) -     { -Dist[i] = edge[1][i]; +Pre[i] =1; -     } +vis[1] =1; Apre[1] = -1; at      for(inti =1; I < n; i++) -     { -         intMinn =Inf,pos; -          for(intj =1; J <= N; J + +) -         { -             if(Vis[j] = =0&& Dist[j] <Minn) in             { -Minn =Dist[j]; topos =J; +             } -         } the         if(Minn = =INF) *         { $Ans =INF;Panax Notoginseng             return; -         } theAns + =Minn; +Vis[pos] =1; AUsed[pos][pre[pos]] = used[Pre[pos]][pos] =true; the          for(intj =1; J <= N; J + +) +         { -             if(Vis[j]) $Maxd[j][pos] = maxd[pos][j] =Max (Dist[pos], Maxd[j][pre[pos]]); $             if(Vis[j] = =0&& Dist[j] >Edge[pos][j]) -             { -DIST[J] =Edge[pos][j]; thePRE[J] =Pos; -             }Wuyi         } the     } - } Wu voidSmst () - { AboutCNT =INF; $      for(inti =1; I <= N; i++) -     { -          for(intj = i +1; J <= N; J + +) -         { A             if(Used[i][j] = =false&& Edge[i][j]! =INF) +             { theCNT = min (cnt, ans-maxd[i][j) +edge[i][j]); -             } $         } the     } the } the intMain () the { -     intT; inscanf"%d", &t); the      while(t--) the     { About          for(inti =1; I <= N; i++) the              for(intj = i +1; J <= N; J + +) the             { theEdge[i][i] =0; +EDGE[I][J] = Edge[j][i] =INF; -             } thescanf"%d%d", &n,&m);Bayi          for(inti =0; I < m; i++) the         { the             intx,y,w; -scanf"%d%d%d", &x, &y, &W); -Edge[x][y] = edge[y][x] =W; the         } theAns =0; the Prime (); the smst (); -         if(ans = =INF) the         { theprintf"Not unique!\n"); the             Continue;94         } the         if(CNT = =ans) the         { theprintf"Not unique!\n");98         } About         Else -         {101printf"%d\n", ans);102         }103     }104     return 0; the}
View Code




Poj1679the Unique MST (sub-niche into a tree)

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.