Sub-niche into a tree: the so-called sub-niche into a tree, refers to the edge set and the edge set of a minimum spanning tree is not exactly the same as the other spanning tree value of the least. Therefore, in numerical values, the smallest spanning tree may be equal to the sub-niche tree, which can also be seen as the minimum number of builds.
Idea: The most intuitive solution is to first find the smallest spanning tree, and record the edges in the smallest spanning tree, and then enumerate the edges of the smallest spanning tree and also the minimum spanning tree.
Take POJ1679 as an example, Kruskal the sub-niche into a tree code as follows:
1#include <cstdio>2#include <cstring>3#include <algorithm>4 #define_CLR (x, y) memset (x, y, sizeof (x))5 #defineINF 0x3f3f3f3f6 #defineN 1107 using namespacestd;8 9 structEdgeTen { One intA, B; A intC; - BOOL operator< (ConstEdge &a)Const - { the returnC <A.C; - } -}edge[n*N]; - intBleg[n], N, m; + intMst_edge[n]; - + intFindintx) A { at inty =x; - while(Y! =Bleg[y]) -y =Bleg[y]; - while(X! =Bleg[x]) - { - intPX =Bleg[x]; inBLEG[X] =y; -x =px; to } + returny; - } the * voidUnion (intAintb) $ {Panax Notoginseng intPa=find (a), pb=find (b); - if(PA! =pb) theBLEG[PA] =PB; + } A theInlinevoidInit () + { - for(intI=0; i<=n; i++) $Bleg[i] =i; $ } - - voidKruskal () the { - intmst_1=0;Wuyi Init (); the_CLR (Mst_edge,0); -Sort (Edge, edge+m); Wu intk=0; - for(intI=0; I<m && k<n-1; i++) About { $ intA=find (EDGE[I].A); - intb=find (edge[i].b); - if(A! =b) - { AMST_EDGE[K] =i; + Union (A, b); theMst_1 + =edge[i].c; -k++; $ } the } the //enumeration deletes all edges in the minimum spanning tree for MST the for(intI=0; i<n-1; i++) the { - intMst_2=0, k=0; in Init (); the for(intj=0; J<m && k<n-1; J + +) the { About if(Mst_edge[i] = = j)Continue; the intA=find (EDGE[J].A); the intb=find (edge[j].b); the if(A! =b) + { - Union (A, b); theMst_2 + =edge[j].c;Bayik++; the } the } - if(Mst_1==mst_2 && k==n-1) - { thePuts"Not unique!"); the return; the } the } -printf"%d\n", mst_1); the } the the intMain ()94 { the intT; thescanf"%d", &T); the while(t--)98 { Aboutscanf"%d%d", &n, &m); - for(intI=0; i<m; i++)101scanf"%d%d%d", &EDGE[I].A, &edge[i].b, &edge[i].c);102 Kruskal ();103 }104 return 0; the}
View Code
Sub-niche into a tree