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<iostream> #include<cstring>
typedef long long ll;
using namespace std;
const int N = 1e5 + 10, M = 2e5 + 20;
int h[N], e[M], ne[M], idx, n;
int far, w[M];
ll cnt;
int st[M];
void add(int a,int b,int c) { e[idx] = b,ne[idx] = h[a], w[idx] = c, h[a] = idx ++; }
void dfs(int u,int dis) { st[u] = 1; if(dis > cnt) { cnt = dis; far = u; } for(int i = h[u];i != -1;i = ne[i]) { int j = e[i]; if(!st[j]) dfs(j,dis + w[i]); } }
void solve() { memset(h,-1,sizeof h);
cin >> n; for(int i = 0;i < n - 1;i ++) { int a, b, c; cin >> a >> b >> c; add(a, b, c); add(b, a, c); } dfs(1,0); memset(st,0,sizeof st); dfs(far,0); cout << cnt * 10 + (cnt + 1) * cnt / 2; }
int main() { solve(); return 0; }
|