main
黄海 2 years ago
parent 232e9bfe5d
commit a86033f36b

@ -2,29 +2,30 @@
#include <cstdio> #include <cstdio>
#include <vector> #include <vector>
#include <cstring> #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; using namespace std;
const int N = 2e5 + 2; const int N = 2e5 + 10;
int T, n, du[N]; int T, n, du[N];
struct node { struct node {
int to, w; int to, w;
}; };
vector<node> c[N]; vector<node> c[N];
bool v[N]; bool v[N];
int d[N], f[N]; int d[N], f[N];
void clean() { void clean() {
mem(v, 0); memset(v, 0, sizeof v);
mem(du, 0); memset(du, 0, sizeof du);
mem(d, 0); memset(d, 0, sizeof d);
mem(f, 0); memset(f, 0, sizeof f);
go(i, 1, n) c[i].clear();
for (int i = 1; i <= n; i++) c[i].clear();
} }
void dp(int x) { void dp(int x) {
v[x] = 1; v[x] = 1;
int size = c[x].size() - 1; int size = c[x].size() - 1;
go(i, 0, size) { for (int i = 0; i <= size; i++) {
int to = c[x][i].to, w = c[x][i].w; int to = c[x][i].to, w = c[x][i].w;
if (v[to]) continue; if (v[to]) continue;
dp(to); dp(to);
@ -38,7 +39,7 @@ void dp(int x) {
void dfs(int x) { void dfs(int x) {
v[x] = 1; v[x] = 1;
int size = c[x].size() - 1; int size = c[x].size() - 1;
go(i, 0, size) { for (int i = 0; i <= size; i++) {
int to = c[x][i].to, w = c[x][i].w; int to = c[x][i].to, w = c[x][i].w;
if (v[to]) continue; if (v[to]) continue;
if (du[x] == 1) if (du[x] == 1)
@ -51,13 +52,13 @@ void dfs(int x) {
} }
int main() { int main() {
scanf("%d", &T); cin >> T;
while (T--) { while (T--) {
scanf("%d", &n); cin >> n;
clean(); clean();
int x, y, z; int x, y, z;
go(i, 1, n - 1) { for (int i = 1; i < n; i++) {
scanf("%d%d%d", &x, &y, &z); cin >> x >> y >> z;
c[x].push_back((node){y, z}); c[x].push_back((node){y, z});
c[y].push_back((node){x, z}); c[y].push_back((node){x, z});
du[x]++; du[x]++;
@ -65,10 +66,10 @@ int main() {
} }
dp(1); dp(1);
f[1] = d[1]; f[1] = d[1];
mem(v, 0); memset(v, 0, sizeof v);
dfs(1); dfs(1);
int ans = 0; int ans = 0;
go(i, 1, n) ans = max(ans, f[i]); for (int i = 1; i <= n; i++) ans = max(ans, f[i]);
printf("%d\n", ans); printf("%d\n", ans);
} }
return 0; return 0;

Loading…
Cancel
Save