1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#include <bits/stdc++.h> using namespace std;
typedef long long LL; const int N = 1e5 + 10; const int M = 2*N;
int n; int h[N],e[M],ne[M],idx=1,w[M]; bool st[N]; LL s[10100]; LL maxu,maxd;
void add(int a,int b,int d){ e[idx] = b; ne[idx] = h[a]; h[a] = idx; w[idx] = d; idx++; }
void dfs(int u,int d){ st[u] = true; for(int i = h[u];i!=-1;i=ne[i]){ int j = e[i]; if(!st[j]){ st[j] = true; if(maxd < d+w[i]){ maxd = d+w[i]; maxu = j; } dfs(j,d+w[i]); st[j] = false; } } }
int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; memset(h,-1,sizeof(h)); for(int i=0;i<n-1;i++){ int a,b,d; cin >> a >> b >> d; add(a,b,d); add(b,a,d); } dfs(1,0); memset(st,0,sizeof(st)); dfs(maxu,0); cout << ( 11 + (11+maxd-1) )*maxd/2; return 0; }
|