Test instructions: In one tree requires one to the other node capacity and the largest point, the capacity before I,j is defined as the minimum edge capacity on the path of I to J.
First thought from small to large to split the edge, but it is difficult to achieve, in fact, the order is easy to do, like Kruskal of a greedy algorithm,
From large to small connecting edge, each connecting two components A and B, so that the new edge capacity must be two components each other to reach the minimum capacity, the other side must be selected maximum, to meet the optimal sub-structure
and the maximum value of the new connected component must be on one side, both of which accesses than either can be selected. Specific implementation and check set maintenance can be.
#include <bits/stdc++.h>using namespacestd;Const intMAXN = 2e5+5; typedefLong Longll;structnode{intCNT; ll sum;} TOWN[MAXN];intPA[MAXN];intFind (intx) {returnx==pa[x]?x:pa[x]=Find (pa[x]);}structedge{intU,v,cap; BOOL operator< (ConstEdge &y)Const { returnCap >Y.cap; }}ROAD[MAXN];#defineInitnode (x) pa[x] = x; town[x].cnt = 1; town[x].sum = 0;intMain () {//freopen ("In.txt", "R", stdin); intN; while(~SCANF ("%d",&N)) { for(inti =1; I < n; i++) {scanf ("%d%d%d",&road[i].u,&road[i].v,&road[i].cap); Initnode (i); } initnode (n); Sort (Road+1, road+N); for(inti =1; I < n; i++){ intx = Find (road[i].u), y =Find (ROAD[I].V); ll T1= Town[x].sum + (LL) town[y].cnt*(ll) Road[i].cap; LL T2= Town[y].sum + (LL) town[x].cnt*(ll) Road[i].cap; if(t1>T2) Swap (x, y), swap (T1,T2); PA[X]=y; Town[y].sum=T2; Town[y].cnt+=town[x].cnt; } printf ("%lld\n", Town[find (1)].sum); } return 0;}
UVA 1664 conquer a New region