Problem p02371 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n;
  scanf("%d", &n);
  vector<vector<pair<int, int>>> g(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    g[a].push_back({b, c});
    g[b].push_back({a, c});
  }
  vector<vector<int>> d(n);
  vector<int> far(n);
  function<void(int, int)> dfs = [&](int u, int prev) {
    for (auto e : g[u])
      if (e.first != prev) {
        int v = e.first;
        dfs(v, u);
        far[u] = max(far[u], far[v] + e.second);
        d[u].push_back(far[v] + e.second);
      }
  };
  dfs(0, -1);
  vector<vector<int>> left(n), right(n);
  auto build = [&](int u) { // d is prepared
    left[u].resize(g[u].size());
    right[u].resize(g[u].size());
    left[u].front() = d[u].front(), right[u].back() = d[u].back();
    for (int i = 0; i < (int)g[u].size() - 1; i++)
      left[u][i + 1] = max(left[u][i], d[u][i + 1]);
    for (int i = (int)g[u].size() - 1; i >= 1; i--)
      right[u][i - 1] = max(right[u][i], d[u][i - 1]);
    return;
  };
  build(0);
  function<void(int, int)> dfs2 = [&](int u, int prev) {
    int idx = 0;
    for (auto e : g[u])
      if (e.first != prev) {
        int v = e.first;
        int ma = 0;
        if (idx - 1 >= 0)
          ma = max(ma, left[u][idx - 1]);
        if (idx + 1 < (int)right[u].size())
          ma = max(ma, right[u][idx + 1]);
        d[v].push_back(ma + e.second);
        build(v);
        dfs2(v, u);
        idx++;
      }
  };
  dfs2(0, -1);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    /*
    for (auto it : d[i]) {
            cerr << it << ' ';
    }
    cerr << endl;
    */
    if (d[i].size() >= 2) {
      sort(d[i].rbegin(), d[i].rend());
      ans = max(ans, d[i][0] + d[i][1]);
    }
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n;
  scanf("%d", &n);
  vector<vector<pair<int, int>>> g(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    g[a].push_back({b, c});
    g[b].push_back({a, c});
  }
  if (n == 1) {
    printf("0\n");
    return 0;
  }
  vector<vector<int>> d(n);
  vector<int> far(n);
  function<void(int, int)> dfs = [&](int u, int prev) {
    for (auto e : g[u])
      if (e.first != prev) {
        int v = e.first;
        dfs(v, u);
        far[u] = max(far[u], far[v] + e.second);
        d[u].push_back(far[v] + e.second);
      }
  };
  dfs(0, -1);
  vector<vector<int>> left(n), right(n);
  auto build = [&](int u) { // d is prepared
    left[u].resize(g[u].size());
    right[u].resize(g[u].size());
    left[u].front() = d[u].front(), right[u].back() = d[u].back();
    for (int i = 0; i < (int)g[u].size() - 1; i++)
      left[u][i + 1] = max(left[u][i], d[u][i + 1]);
    for (int i = (int)g[u].size() - 1; i >= 1; i--)
      right[u][i - 1] = max(right[u][i], d[u][i - 1]);
    return;
  };
  build(0);
  function<void(int, int)> dfs2 = [&](int u, int prev) {
    int idx = 0;
    for (auto e : g[u])
      if (e.first != prev) {
        int v = e.first;
        int ma = 0;
        if (idx - 1 >= 0)
          ma = max(ma, left[u][idx - 1]);
        if (idx + 1 < (int)right[u].size())
          ma = max(ma, right[u][idx + 1]);
        d[v].push_back(ma + e.second);
        build(v);
        dfs2(v, u);
        idx++;
      }
  };
  dfs2(0, -1);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    /*
    for (auto it : d[i]) {
            cerr << it << ' ';
    }
    cerr << endl;
    */
    if (d[i].size() >= 2) {
      sort(d[i].rbegin(), d[i].rend());
      ans = max(ans, d[i][0] + d[i][1]);
    }
  }
  printf("%d\n", ans);
  return 0;
}