You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
using namespace std;
|
|
|
|
|
const int N = 2e5 + 10, M = N << 1;
|
|
|
|
|
|
|
|
|
|
int n, du[N];
|
|
|
|
|
|
|
|
|
|
// 链式前向星
|
|
|
|
|
int e[M], h[N], idx, w[M], ne[M];
|
|
|
|
|
void add(int a, int b, int c = 0) {
|
|
|
|
|
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int f[N], g[N];
|
|
|
|
|
|
|
|
|
|
// 以1号点为根, 由子推父,先递归,再统计
|
|
|
|
|
void dfs1(int u, int fa) {
|
|
|
|
|
for (int i = h[u]; ~i; i = ne[i]) {
|
|
|
|
|
int v = e[i];
|
|
|
|
|
if (v == fa) continue;
|
|
|
|
|
dfs1(v, u);
|
|
|
|
|
if (du[v] == 1) // 如果v的度为1,也就是叶子
|
|
|
|
|
f[u] += w[i];
|
|
|
|
|
else
|
|
|
|
|
f[u] += min(f[v], w[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void dfs2(int u, int fa) {
|
|
|
|
|
for (int i = h[u]; ~i; i = ne[i]) {
|
|
|
|
|
int v = e[i];
|
|
|
|
|
if (v == fa) continue;
|
|
|
|
|
if (du[u] == 1)
|
|
|
|
|
g[v] = f[v] + w[i];
|
|
|
|
|
else
|
|
|
|
|
g[v] = f[v] + min(g[u] - min(f[v], w[i]), w[i]);
|
|
|
|
|
// 先计算再递归
|
|
|
|
|
dfs2(v, u);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
// 加快读入
|
|
|
|
|
ios::sync_with_stdio(false), cin.tie(0);
|
|
|
|
|
int T;
|
|
|
|
|
cin >> T;
|
|
|
|
|
while (T--) {
|
|
|
|
|
// 初始化链式前向星
|
|
|
|
|
memset(h, -1, sizeof h);
|
|
|
|
|
idx = 0;
|
|
|
|
|
memset(du, 0, sizeof du);
|
|
|
|
|
memset(f, 0, sizeof f);
|
|
|
|
|
memset(g, 0, sizeof g);
|
|
|
|
|
|
|
|
|
|
cin >> n;
|
|
|
|
|
|
|
|
|
|
int a, b, c;
|
|
|
|
|
for (int i = 1; i < n; i++) { // 树,n-1条无向边
|
|
|
|
|
cin >> a >> b >> c;
|
|
|
|
|
add(a, b, c), add(b, a, c);
|
|
|
|
|
du[a]++, du[b]++; // 记录入度,无向图就不谈入度和出度了
|
|
|
|
|
}
|
|
|
|
|
// 第一遍dfs
|
|
|
|
|
dfs1(1, 0);
|
|
|
|
|
g[1] = f[1];
|
|
|
|
|
|
|
|
|
|
// 第二遍dfs
|
|
|
|
|
dfs2(1, 0);
|
|
|
|
|
|
|
|
|
|
int ans = 0;
|
|
|
|
|
for (int i = 1; i <= n; i++) ans = max(ans, g[i]);
|
|
|
|
|
cout << ans << endl;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|