Problem p03941 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>

using namespace std;
typedef long long ll;
typedef pair<int, int> Pi;
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define sz(x) (int)x.size()
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) x.begin(), x.end()

int N;
vector<Pi> E[100010], D[100010];

void dfs(int x, int L) {
  if (sz(E[x]) == 0)
    return;
  Pi A = E[x][0], B = E[x][1];
  dfs(A.Se, L);
  dfs(B.Se, L);
  vector<Pi> v;
  if (sz(D[A.Se]) > sz(D[B.Se]))
    swap(A, B);
  int L2 = A.Fi + B.Fi;
  for (int i = 0, j = 0; i < sz(D[A.Se]); i++) {
    while (j < sz(D[B.Se]) && D[B.Se][j].Fi + D[A.Se][i].Se + L2 <= L)
      ++j;
    if (j != 0) {
      v.emplace_back(D[A.Se][i].Fi + A.Fi, D[B.Se][j - 1].Se + B.Fi);
    }
  }
  for (int i = 0, j = 0; i < sz(D[A.Se]); i++) {
    while (j < sz(D[B.Se]) && D[B.Se][j].Se + D[A.Se][i].Fi + L2 > L)
      ++j;
    if (j != sz(D[B.Se])) {
      v.emplace_back(D[B.Se][j].Fi + B.Fi, D[A.Se][i].Se + A.Fi);
    }
  }
  sort(all(v));
  int now = ~0u >> 1;
  for (int i = 0; i < sz(v); i++) {
    if (i > 0 && v[i].Fi == v[i - 1].Fi)
      continue;
    if (now > v[i].Se) {
      D[x].pb(v[i]);
      now = v[i].Se;
    }
  }
}

int Do(int L) {
  for (int i = 1; i <= N; i++)
    if (sz(E[i]) == 0)
      D[i].emplace_back(0, 0);
  dfs(1, L);
  int save = sz(D[1]);
  for (int i = 1; i <= N; i++)
    D[i].clear();
  return save != 0;
}

void solve() {
  scanf("%d", &N);
  for (int i = 2; i <= N; i++) {
    int a, v;
    scanf("%d%d", &a, &v);
    E[a].pb(Pi(v, i));
  }

  int low = 0, high = 1e9, res = -1;
  while (low <= high) {
    int mid = (low + high) >> 1;
    if (Do(mid))
      res = mid, high = mid - 1;
    else
      low = mid + 1;
  }
  printf("%d", res);
}

int main() {
  int Tc = 1; // scanf("%d\n", &Tc);
  for (int tc = 1; tc <= Tc; tc++) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>

using namespace std;
typedef long long ll;
typedef pair<int, int> Pi;
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define sz(x) (int)x.size()
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) x.begin(), x.end()

int N;
vector<Pi> E[150010], D[150010];

void dfs(int x, int L) {
  if (sz(E[x]) == 0)
    return;
  Pi A = E[x][0], B = E[x][1];
  dfs(A.Se, L);
  dfs(B.Se, L);
  vector<Pi> v;
  if (sz(D[A.Se]) > sz(D[B.Se]))
    swap(A, B);
  int L2 = A.Fi + B.Fi;
  for (int i = 0, j = 0; i < sz(D[A.Se]); i++) {
    while (j < sz(D[B.Se]) && D[B.Se][j].Fi + D[A.Se][i].Se + L2 <= L)
      ++j;
    if (j != 0) {
      v.emplace_back(D[A.Se][i].Fi + A.Fi, D[B.Se][j - 1].Se + B.Fi);
    }
  }
  for (int i = 0, j = 0; i < sz(D[A.Se]); i++) {
    while (j < sz(D[B.Se]) && D[B.Se][j].Se + D[A.Se][i].Fi + L2 > L)
      ++j;
    if (j != sz(D[B.Se])) {
      v.emplace_back(D[B.Se][j].Fi + B.Fi, D[A.Se][i].Se + A.Fi);
    }
  }
  sort(all(v));
  int now = ~0u >> 1;
  for (int i = 0; i < sz(v); i++) {
    if (i > 0 && v[i].Fi == v[i - 1].Fi)
      continue;
    if (now > v[i].Se) {
      D[x].pb(v[i]);
      now = v[i].Se;
    }
  }
}

int Do(int L) {
  for (int i = 1; i <= N; i++)
    if (sz(E[i]) == 0)
      D[i].emplace_back(0, 0);
  dfs(1, L);
  int save = sz(D[1]);
  for (int i = 1; i <= N; i++)
    D[i].clear();
  return save != 0;
}

void solve() {
  scanf("%d", &N);
  for (int i = 2; i <= N; i++) {
    int a, v;
    scanf("%d%d", &a, &v);
    E[a].pb(Pi(v, i));
  }

  int low = 0, high = 1e9, res = -1;
  while (low <= high) {
    int mid = (low + high) >> 1;
    if (Do(mid))
      res = mid, high = mid - 1;
    else
      low = mid + 1;
  }
  printf("%d", res);
}

int main() {
  int Tc = 1; // scanf("%d\n", &Tc);
  for (int tc = 1; tc <= Tc; tc++) {
    solve();
  }
  return 0;
}