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 T, 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 d[N], f[N];
|
|
|
|
|
|
|
|
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)
|
|
|
|
d[u] += w[i];
|
|
|
|
else
|
|
|
|
d[u] += min(d[v], w[i]);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
f[v] = d[v] + w[i];
|
|
|
|
else
|
|
|
|
f[v] = d[v] + min(f[u] - min(d[v], w[i]), w[i]);
|
|
|
|
dfs2(v, u);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
// 加快读入
|
|
|
|
ios::sync_with_stdio(false), cin.tie(0);
|
|
|
|
cin >> T;
|
|
|
|
while (T--) {
|
|
|
|
// 初始化链式前向星
|
|
|
|
memset(h, -1, sizeof h);
|
|
|
|
idx = 0;
|
|
|
|
memset(du, 0, sizeof du);
|
|
|
|
memset(d, 0, sizeof d);
|
|
|
|
memset(f, 0, sizeof f);
|
|
|
|
|
|
|
|
int a, b, c;
|
|
|
|
for (int i = 1; i < n; i++) {
|
|
|
|
cin >> a >> b >> c;
|
|
|
|
add(a, b, c), add(b, a, c);
|
|
|
|
du[a]++, du[b]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
cin >> n;
|
|
|
|
|
|
|
|
dfs1(1, 0);
|
|
|
|
f[1] = d[1];
|
|
|
|
dfs2(1, 0);
|
|
|
|
|
|
|
|
int ans = 0;
|
|
|
|
for (int i = 1; i <= n; i++) ans = max(ans, f[i]);
|
|
|
|
printf("%d\n", ans);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|