usingnamespacestd; constint N = 1e5 + 10; int head[N], edge[N * 2], ver[N * 2], Next[N * 2], n, tot; int vis[N], dis[N], res;
voidadd(int u, int v, int w) { edge[tot] = v; ver[tot] = w; Next[tot] = head[u]; head[u] = tot++; }
voiddfs(int x, int distance) { dis[x] = distance; vis[x] = 1; for(int i = head[x]; ~i; i = Next[i]){ int j = edge[i]; if(vis[j]) continue; dfs(j, distance + ver[i]); } }
intmain() { cin >> n; memset(head, -1, sizeof head); for(int i = 0; i < n - 1; i++){ int u, v, w; cin >> u >> v >> w; add(u, v, w), add(v, u, w); } dfs(1, 0); int u = 1; for(int i = 1; i <= n; i++) if(dis[i] > dis[u]) u = i;
memset(vis, 0, sizeof vis); dfs(u, 0); for(int i = 1; i <= n; i++) res = max(res, dis[i]);
printf("%lld\n", res * 11 + res * (res - 1ll) / 2); return0; }
usingnamespacestd; constint N = 1e5 + 10; int head[N], edge[N * 2], ver[N * 2], Next[N * 2], n, tot; int vis[N], dis[N], res;
voidadd(int u, int v, int w) { edge[tot] = v; ver[tot] = w; Next[tot] = head[u]; head[u] = tot++; }
voiddfs(int x, int father, int distance) { dis[x] = distance; for(int i = head[x]; ~i; i = Next[i]){ int y = edge[i]; if(y == father) continue; dfs(y, x, distance + ver[i]); } }
intmain() { cin >> n; memset(head, -1, sizeof head); for(int i = 0; i < n - 1; i++){ int u, v, w; cin >> u >> v >> w; add(u, v, w), add(v, u, w); } dfs(1, -1, 0); int u = 1; for(int i = 1; i <= n; i++) if(dis[i] > dis[u]) u = i;
dfs(u, -1, 0); for(int i = 1; i <= n; i++) res = max(res, dis[i]);
printf("%lld\n", res * 11 + res * (res - 1ll) / 2); return0; }
usingnamespacestd; constint N = 1e5 + 10; int head[N], edge[N * 2], ver[N * 2], Next[N * 2], n, tot; int vis[N], dis[N], res;
voidadd(int u, int v, int w) { edge[tot] = v; ver[tot] = w; Next[tot] = head[u]; head[u] = tot++; }
intbfs(int u) { queue<int> q; memset(vis, 0, sizeof vis); memset(dis, 0, sizeof dis); int max_i; q.push(u); vis[u] = 1; while(q.size()){ int x = q.front(); q.pop(); for(int i = head[x]; ~i; i = Next[i]){ int y = edge[i]; if(vis[y]) continue; dis[y] = dis[x] + ver[i]; if(dis[y] > res){ res = dis[y]; max_i = y; } vis[y] = 1; q.push(y); } } return max_i; }
intmain() { cin >> n; memset(head, -1, sizeof head); for(int i = 0; i < n - 1; i++){ int u, v, w; cin >> u >> v >> w; add(u, v, w), add(v, u, w); } int u = bfs(1); bfs(u);
printf("%lld\n", res * 11 + res * (res - 1ll) / 2); return0; }