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.
47 lines
1.0 KiB
47 lines
1.0 KiB
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
const int N = 10010, M = 20010;
|
|
|
|
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++;
|
|
}
|
|
int st[N];
|
|
|
|
void dfs(int u, int sum) {
|
|
st[u] = 1;
|
|
if (sum > ans) {
|
|
ans = sum; // 记录最大距离
|
|
t = u; // 记录最远的点t1
|
|
}
|
|
for (int i = h[u]; ~i; i = ne[i]) {
|
|
int j = e[i];
|
|
if (st[j]) continue;
|
|
dfs(j, 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);
|
|
}
|
|
memset(st, 0, sizeof st);
|
|
dfs(1, 0); // 先找到点距离点1最远的点t1
|
|
|
|
memset(st, 0, sizeof st);
|
|
dfs(t, 0); // 找到距离点t1->t2最远的点t1
|
|
|
|
printf("%d", ans);
|
|
return 0;
|
|
} |