fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n, bb[200005], way[10005][10005], res, ans = 1e9, visited[10005][10005], val[200005];
  4. vector<int> adj[200005];
  5.  
  6. void kahn() {
  7. queue<int> q;
  8. for (int i = 1; i <= n; i++)
  9. if (bb[i] == 1)
  10. q.push(i);
  11. int u;
  12. while (!q.empty()) {
  13. u = q.front();
  14. q.pop();
  15. for (int v : adj[u]) {
  16. if (bb[v] > 1)
  17. val[v] = max(val[v], val[u] + way[u][v]);
  18. bb[v]--;
  19. bb[u]--;
  20. if (bb[v] == 1)
  21. q.push(v), visited[u][v] = visited[v][u] = 1;
  22. }
  23. }
  24.  
  25. for (int v : adj[u])
  26. res = res + val[v] + way[v][u];
  27. for (int v : adj[u])
  28. ans = min(ans, max(res - (way[v][u]+val[v]), val[v]));
  29. cout << ans;
  30. }
  31.  
  32. int main() {
  33. ios_base::sync_with_stdio(false);
  34. cin.tie(nullptr);
  35. freopen("diameter.inp", "r", stdin);
  36. freopen("diameter.out", "w", stdout);
  37. cin >> n;
  38. for (int i = 1; i < n; i++) {
  39. int x, y, z;
  40. cin >> x >> y >> z;
  41. adj[x].push_back(y);
  42. adj[y].push_back(x);
  43. bb[x]++;
  44. bb[y]++;
  45. way[x][y] = way[y][x] = z;
  46. }
  47. kahn();
  48.  
  49. return 0;
  50. }
  51.  
Success #stdin #stdout 0.01s 9572KB
stdin
Standard input is empty
stdout
Standard output is empty