1、题意:给个树,边的权值=两边的点数差*此边的长度,求所有边的权值和
2、分析:真不想说啥了。。。dfs即可
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> using namespace std; #define LL long long #define M 2000010 inline int read(){ char ch = getchar(); int x = 0, f = 1; while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); } while('0' <= ch && ch <= '9'){ x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } struct Edge{ int u, v, w, next; } G[M]; int head[M], tot; int size[M]; LL ans; int n; inline void add(int u, int v, int w){ G[++ tot] = (Edge){u, v, w, head[u]}; head[u] = tot; } inline void dfs(int x, int fa){ size[x] = 1; for(int i = head[x]; i != -1; i = G[i].next) if(G[i].v != fa){ dfs(G[i].v, x); size[x] += size[G[i].v]; ans += (LL)abs(n - size[G[i].v] - size[G[i].v]) * (LL)G[i].w; } } int main(){ n = read(); memset(head, -1, sizeof(head)); for(int i = 1; i < n; i ++){ int u = read(), v = read(), w = read(); add(u, v, w); add(v, u, w); } dfs(1, 0); printf("%lld\n", ans); return 0; }