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
2
7 ·
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!
Test instructions is whether the smallest spanning tree of a graph is unique.
The idea is to find the sub-niche into a tree, judge the sub-niche into a tree's weight and the minimum spanning tree is equal. The second niche into a tree is to enumerate each edge of the smallest spanning tree, remove one of the edges, find the other edges on the two points, and the remaining edges to form the smallest spanning tree
#include <iostream>#include <algorithm>#include <cstdio>#define MAXNusing namespace STD;structnode{intX,y,w;BOOLFlag;}; Node EDGE[MAXN*MAXN];BOOLCMP (Node A,node b) {returnA.W<B.W;}intN,M,FATHER[MAXN];intKruskal (intNumintm) {intans=0, cnt=1; for(intI=1; i<=m; ++i) {if(I==num)//Remove the NUM side of the first minimum generated number Continue;intS1=father[edge[i].x];intS2=FATHER[EDGE[I].Y];if(S1!=S2) {cnt++; ANS+=EDGE[I].W; FATHER[S2]=S1;//Join the edge of the spanning tree and add the same set of checks for(intj=0; j<=n; ++J)if(FATHER[J]==S2) father[j]=s1; } }if(cnt!=n)return-1;Else returnAns;}intMain () {intT,ans;scanf("%d", &t); while(t--) {scanf("%d%d", &n,&m); for(intI=1; i<=n; ++i) father[i]=i; for(intI=1; i<=m; ++i) {scanf("%d%d%d", &EDGE[I].X,&EDGE[I].Y,&EDGE[I].W); edge[i].flag=false; } sort (edge+1, edge+1+M,CMP); ans=0; for(intI=1; i<=m; ++i) {intS1=father[edge[i].x];intS2=FATHER[EDGE[I].Y];if(S1!=S2) {edge[i].flag=true;//Minimum number of generated tokensANS+=EDGE[I].W; FATHER[S2]=S1; for(intj=0; j<=n; ++J)if(FATHER[J]==S2) father[j]=s1; } }BOOLflag=0; for(intI=1; i<=m; ++i)//Enumerate each edge of the minimum spanning tree{if(edge[i].flag==false)Continue;intsum=0; for(intj=1; j<=n; ++J) Father[j]=j; Sum=kruskal (I,M);//Remove the Edge of article I if(Sum==ans) {flag=true; Break; } }if(flag)printf("Not unique!\n");Else printf("%d\n", ans); }return 0;}
Poj1679--the Unique MST (sub-niche into a tree, Kruskal)