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.

76 lines
1.6 KiB

2 years ago
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
2 years ago
const int N = 2e5 + 10;
2 years ago
int T, n, du[N];
2 years ago
2 years ago
struct node {
int to, w;
};
vector<node> c[N];
2 years ago
2 years ago
bool v[N];
int d[N], f[N];
2 years ago
2 years ago
void clean() {
2 years ago
memset(v, 0, sizeof v);
memset(du, 0, sizeof du);
memset(d, 0, sizeof d);
memset(f, 0, sizeof f);
for (int i = 1; i <= n; i++) c[i].clear();
2 years ago
}
void dp(int x) {
v[x] = 1;
int size = c[x].size() - 1;
2 years ago
for (int i = 0; i <= size; i++) {
2 years ago
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;
2 years ago
for (int i = 0; i <= size; i++) {
2 years ago
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() {
2 years ago
cin >> T;
2 years ago
while (T--) {
2 years ago
cin >> n;
2 years ago
clean();
int x, y, z;
2 years ago
for (int i = 1; i < n; i++) {
cin >> x >> y >> z;
2 years ago
c[x].push_back((node){y, z});
c[y].push_back((node){x, z});
du[x]++;
du[y]++;
}
dp(1);
f[1] = d[1];
2 years ago
memset(v, 0, sizeof v);
2 years ago
dfs(1);
int ans = 0;
2 years ago
for (int i = 1; i <= n; i++) ans = max(ans, f[i]);
2 years ago
printf("%d\n", ans);
}
return 0;
}