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.
|
|
|
|
#include <bits/stdc++.h>
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
const int N = 10010; // 点数上限
|
|
|
|
|
const int M = N * 2; // 边数上限
|
|
|
|
|
int n;
|
|
|
|
|
int ans;
|
|
|
|
|
int d1[N], d2[N]; // 最长,次长
|
|
|
|
|
int st[N];
|
|
|
|
|
int h[N], e[M], w[M], ne[M], idx;
|
|
|
|
|
void add(int a, int b, int c) {
|
|
|
|
|
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void dfs(int u) {
|
|
|
|
|
st[u] = 1;
|
|
|
|
|
for (int i = h[u]; ~i; i = ne[i]) {
|
|
|
|
|
int j = e[i];
|
|
|
|
|
if (st[j]) continue;
|
|
|
|
|
|
|
|
|
|
// 走j子树,完成后,j子树中每个节点的d1[j],d2[j]都已经准备好,u节点可以直接利用
|
|
|
|
|
dfs(j);
|
|
|
|
|
|
|
|
|
|
// d1[u]:最长路径,d2[u]:次长路径
|
|
|
|
|
if (d1[j] + w[i] >= d1[u])
|
|
|
|
|
d2[u] = d1[u], d1[u] = d1[j] + w[i]; // 最长路转移
|
|
|
|
|
else if (d1[j] + w[i] > d2[u])
|
|
|
|
|
d2[u] = d1[j] + w[i]; // 次长路转移
|
|
|
|
|
}
|
|
|
|
|
// 更新结果
|
|
|
|
|
ans = max(ans, d1[u] + d2[u]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
cin >> n;
|
|
|
|
|
// 初始化邻接表
|
|
|
|
|
memset(h, -1, sizeof h);
|
|
|
|
|
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);
|
|
|
|
|
// 输出答案
|
|
|
|
|
printf("%d", ans);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|