Problem p01461 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

const int MX = 500001;
int n, l[MX], r[MX], lf[MX], depth[MX];
ll dp[2][MX];

inline void rec(int c, int d = 0) {
  // if(c == n) return;
  // rec(l[c], d + 1); rec(r[c], d + 1);
  lf[c] += lf[l[c]] + lf[r[c]];
  dp[0][c] += dp[0][l[c]] + lf[l[c]];
  dp[0][c] += dp[0][r[c]] + lf[r[c]];

  dp[1][c] = min(lf[l[c]] + dp[0][l[c]] + dp[1][r[c]],
                 lf[r[c]] + dp[1][l[c]] + dp[0][r[c]]) +
             1;
  dp[1][c] = min(dp[1][c], 2 + d + dp[1][l[c]] + dp[1][r[c]]);
}

int main() {
  cin >> n;
  rep(i, n - 1) cin >> l[i + 1] >> r[i + 1];

  vi ord;
  queue<pi> q;
  q.push(mp(1, 0));
  while (!q.empty()) {
    int c = q.front().first, d = q.front().second;
    q.pop();
    ord.pb(c);
    depth[c] = d;
    if (l[c] < n)
      q.push(mp(l[c], d + 1));
    if (r[c] < n)
      q.push(mp(r[c], d + 1));
  }
  reverse(all(ord));

  lf[n] = 1;
  rep(i, n - 1) rec(ord[i], depth[ord[i]]);
  cout << dp[1][1] << endl;
  /*
  rep(i, n) cerr<<dp[0][i + 1] << " " << dp[1][i + 1] << endl;
  rep(i, n) dbg(lf[i + 1]);
  */
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

const int MX = 50001;
int n, l[MX], r[MX], lf[MX], depth[MX];
ll dp[2][MX];

inline void rec(int c, int d = 0) {
  // if(c == n) return;
  // rec(l[c], d + 1); rec(r[c], d + 1);
  lf[c] += lf[l[c]] + lf[r[c]];
  dp[0][c] += dp[0][l[c]] + lf[l[c]];
  dp[0][c] += dp[0][r[c]] + lf[l[c]];

  dp[1][c] = min(lf[l[c]] + dp[0][l[c]] + dp[1][r[c]],
                 lf[r[c]] + dp[1][l[c]] + dp[0][r[c]]) +
             1;
  dp[1][c] = min(dp[1][c], 2 + d + dp[1][l[c]] + dp[1][r[c]]);
}

int main() {
  cin >> n;
  rep(i, n - 1) cin >> l[i + 1] >> r[i + 1];

  vi ord;
  queue<pi> q;
  q.push(mp(1, 0));
  while (!q.empty()) {
    int c = q.front().first, d = q.front().second;
    q.pop();
    ord.pb(c);
    depth[c] = d;
    if (l[c] < n)
      q.push(mp(l[c], d + 1));
    if (r[c] < n)
      q.push(mp(r[c], d + 1));
  }
  reverse(all(ord));

  lf[n] = 1;
  rep(i, n - 1) rec(ord[i], depth[ord[i]]);
  cout << dp[1][1] << endl;
  /*
  rep(i, n) cerr<<dp[0][i + 1] << " " << dp[1][i + 1] << endl;
  rep(i, n) dbg(lf[i + 1]);
  */
  return 0;
}