parent
34c9fee705
commit
232e9bfe5d
@ -0,0 +1,75 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <vector>
|
||||||
|
#include <cstring>
|
||||||
|
#define rg register
|
||||||
|
#define go(i, a, b) for (rg int i = a; i <= b; i++)
|
||||||
|
#define mem(a, b) memset(a, b, sizeof(a))
|
||||||
|
using namespace std;
|
||||||
|
const int N = 2e5 + 2;
|
||||||
|
int T, n, du[N];
|
||||||
|
struct node {
|
||||||
|
int to, w;
|
||||||
|
};
|
||||||
|
vector<node> c[N];
|
||||||
|
bool v[N];
|
||||||
|
int d[N], f[N];
|
||||||
|
void clean() {
|
||||||
|
mem(v, 0);
|
||||||
|
mem(du, 0);
|
||||||
|
mem(d, 0);
|
||||||
|
mem(f, 0);
|
||||||
|
go(i, 1, n) c[i].clear();
|
||||||
|
}
|
||||||
|
void dp(int x) {
|
||||||
|
v[x] = 1;
|
||||||
|
int size = c[x].size() - 1;
|
||||||
|
go(i, 0, size) {
|
||||||
|
int to = c[x][i].to, w = c[x][i].w;
|
||||||
|
if (v[to]) continue;
|
||||||
|
dp(to);
|
||||||
|
if (du[to] == 1)
|
||||||
|
d[x] += w;
|
||||||
|
else
|
||||||
|
d[x] += min(d[to], w);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
void dfs(int x) {
|
||||||
|
v[x] = 1;
|
||||||
|
int size = c[x].size() - 1;
|
||||||
|
go(i, 0, size) {
|
||||||
|
int to = c[x][i].to, w = c[x][i].w;
|
||||||
|
if (v[to]) continue;
|
||||||
|
if (du[x] == 1)
|
||||||
|
f[to] = d[to] + w;
|
||||||
|
else
|
||||||
|
f[to] = d[to] + min(f[x] - min(d[to], w), w);
|
||||||
|
dfs(to);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
scanf("%d", &T);
|
||||||
|
while (T--) {
|
||||||
|
scanf("%d", &n);
|
||||||
|
clean();
|
||||||
|
int x, y, z;
|
||||||
|
go(i, 1, n - 1) {
|
||||||
|
scanf("%d%d%d", &x, &y, &z);
|
||||||
|
c[x].push_back((node){y, z});
|
||||||
|
c[y].push_back((node){x, z});
|
||||||
|
du[x]++;
|
||||||
|
du[y]++;
|
||||||
|
}
|
||||||
|
dp(1);
|
||||||
|
f[1] = d[1];
|
||||||
|
mem(v, 0);
|
||||||
|
dfs(1);
|
||||||
|
int ans = 0;
|
||||||
|
go(i, 1, n) ans = max(ans, f[i]);
|
||||||
|
printf("%d\n", ans);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in new issue