Give you a tree each side has a weighted value select one point for center define S value as center to other n-1 points on the path of the minimum edge to seek all points s value of the and
From large to small sort each merge 2 tree set to a set B set set a set of the maximum s value of the and for Suma b set for Sumb
Center in A or b now join a A-c this edge makes 2 sets connected because the weight of the side of a-a is less than equal to the weight of the inner edge of the AB set so if the center is in a then SumA = Suma+b set of points *a-b the weight of this edge Sumb = number of points in the Sumb+a collection * The weight of this side of a-B is greater than 2.
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <cmath
> Using namespace std;
const int MAXN = 200010;
int n;
int F[MAXN], RANK[MAXN];
__int64 SUM[MAXN]; struct Edge {int u, V, W;}
E[MAXN];
int find (int x) {if (x = f[x]) return f[x] = find (f[x]);
return f[x]; } BOOL CMP (Edge A, Edge b) {return A.W > B.W;} int main () {while (scanf ("%d", &n)! = EOF) {for (int i = 0; i < n-1;
i++) scanf ("%d%d%d", &e[i].u, &E[I].V, &E[I].W);
Sort (E, e+n-1, CMP);
for (int i = 0; I <= N; i++) F[i] = i, sum[i] = 0, rank[i] = 1;
for (int i = 0; i < n-1; i++) {int x = find (E[I].U);
int y = find (E[I].V);
if (x! = y) {if (sum[x]+ (__int64) e[i].w*rank[y] > sum[y]+ (__int64) e[i].w*rank[x]) {f[y] = x;
SUM[X] = sum[x]+ (__int64) e[i].w*rank[y];
RANK[X] + = Rank[y];
} else {f[x] = y;
Sum[y] = sum[y]+ (__int64) e[i].w*rank[x]; Rank[y] + = RANK[X];
}}} int x = find (1);
printf ("%i64d\n", sum[x]);
} return 0; }