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.
43 lines
959 B
43 lines
959 B
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
const int N = 10010, M = N << 1;
|
|
|
|
int ans; // 保存最长路径
|
|
int t; // 保存找到的最远点
|
|
int 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++;
|
|
}
|
|
|
|
void dfs(int u, int fa, int sum) {
|
|
if (sum > ans) {
|
|
ans = sum; // 记录最大距离
|
|
t = u; // 记录最远的点t1
|
|
}
|
|
for (int i = h[u]; ~i; i = ne[i]) {
|
|
int v = e[i];
|
|
if (v == fa) continue;
|
|
dfs(v, u, sum + w[i]);
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
memset(h, -1, sizeof h);
|
|
cin >> n;
|
|
for (int i = 1; i < n; i++) {
|
|
int a, b, c;
|
|
cin >> a >> b >> c;
|
|
add(a, b, c), add(b, a, c);
|
|
}
|
|
dfs(1, 0, 0); // 先找到点距离点1最远的点t1
|
|
|
|
dfs(t, 0, 0); // 找到距离点t1->t2最远的点t1
|
|
|
|
printf("%d", ans);
|
|
return 0;
|
|
} |