The Unique MST
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 26454 |
|
Accepted: 9457 |
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: Determine if the minimum spanning tree is unique
Solving the puzzle: directly seeking secondary niche into a tree.
Sub-niche into a tree to find a way to poke here: http://blog.csdn.net/niushuai666/article/details/6925258
#include <stdio.h>#include<iostream>#include<string.h>#include<algorithm>#include<math.h>using namespacestd;Const intN =505;Const intINF =99999999;intGraph[n][n];intn,m;intPath[n][n],pre[n],low[n];///Path[i][j] used to record the most weighted edge on the I-to-J PathBOOLVis[n],used[n][n];intPrimintPosintN) {memset (used,false,sizeof(used)); memset (Vis,false,sizeof(VIS)); memset (Path,0,sizeof(path)); Vis[pos]=true; intCost =0; for(intI=1; i<=n;i++) {Low[i]=Graph[pos][i]; Pre[i]=1; } Low[pos]=1; for(intI=1; i<n;i++){ intMin =INF; for(intj=1; j<=n;j++){ if(!vis[j]&&low[j]<Min) {POS=J; Min=Low[j]; }} Used[pre[pos]][pos]= Used[pos][pre[pos]] =true; Cost+=Min; Vis[pos]=true; for(intj=1; j<=n;j++){ if(Vis[j]&&j!=pos) {///The maximum right edge from the Pos-j pathPATH[POS][J] = Path[j][pos] =Max (Low[pos],path[j][pre[pos]]); } if(!vis[j]&&low[j]>Graph[pos][j]) {Low[j]=Graph[pos][j]; PRE[J]=POS; } } } returnCost ;}intMain () {inttcase; scanf ("%d",&tcase); while(tcase--) {scanf ("%d%d",&n,&m); for(intI=1; i<=n;i++){ for(intj=1; j<=n;j++) { if(I==J) graph[i][j]=0; ElseGRAPH[I][J] =INF; } } for(intI=0; i<m;i++){ intA,b,c; scanf ("%d%d%d",&a,&b,&c); GRAPH[A][B]= Graph[b][a] =C; } intCost = Prim (1, N); intres =INF; for(intI=1; i<=n;i++){ for(intj=i+1; j<=n;j++){ if(!used[i][j]) res = min (res,cost+graph[i][j]-Path[i][j]); } } if(res==cost) printf ("Not unique!\n"); Elseprintf"%d\n", cost); }}
POJ 1679 (sub-niche into a tree)