#include<iostream>
#include<cstring>
using namespace std;
const int N = 100010,M=200010;
int h[N],e[M],ne[M],w[M],idx;
int n;
int dist[N];
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,int father,int distance){
dist[u] = distance;
for(int i=h[u];i!=-1;i=ne[i]){
int j=e[i];
if(j!=father){
dfs(j,u,distance+w[i]);
}
}
}
int main(){
scanf("%d",&n);
memset(h,-1,sizeof h);
for(int i=0;i<n-1;i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
dfs(1,-1,0);
int u = 1;
for(int i=2;i<=n;i++){
if(dist[u]<dist[i]) u=i;
}
dfs(u,-1,0);
for(int i=1;i<=n;i++){
if(dist[u]<dist[i]) u=i;
}
int s = dist[u];
printf("%lld",s*10+(s+1ll)*s/2);
}
大臣的旅费(c++实现)
最新推荐文章于 2024-03-16 20:53:19 发布