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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const ll INF = LLONG_MAX / 10;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int n;
vector<pii> tree;
vi leaves, nosave, ans, depth;

int main() {
  cin.sync_with_stdio(false);
  cout << fixed << setprecision(10);
  cin >> n;
  tree.resize(n);
  REP(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (a == n - 1)
      a = n - 1;
    if (b == n - 1)
      b = n - 1;
    tree[i] = pii(a, b);
  }
  vi per;
  stack<int> st;
  st.push(0);
  while (!st.empty()) {
    int v = st.top();
    st.pop();
    per.push_back(v);
    if (v == n - 1)
      continue;
    st.push(tree[v].first);
    st.push(tree[v].second);
  }
  depth.resize(n);
  leaves.resize(n);
  nosave.resize(n);
  ans.resize(n);
  REP(i, per.size()) {
    int v = per[i];
    if (v == n - 1)
      continue;
    int l = tree[v].first, r = tree[v].second;
    depth[l] = depth[v] + 1;
    depth[r] = depth[v] + 1;
  }
  reverse(ALL(per));
  REP(i, per.size()) {
    int v = per[i];
    if (v == n - 1) {
      leaves[v] = 1;
      nosave[v] = 0;
    } else {
      int l = tree[v].first, r = tree[v].second;
      leaves[v] = leaves[l] + leaves[r];
      nosave[v] = nosave[l] + nosave[r] + leaves[v];
    }
  }
  REP(i, per.size()) {
    int v = per[i];
    if (v == n - 1)
      ans[v] = 0;
    else {
      ans[v] = INF;
      int l = tree[v].first, r = tree[v].second;
      chmin(ans[v], nosave[l] + leaves[l] + ans[r] + 1);
      chmin(ans[v], ans[l] + nosave[r] + leaves[r] + 1);
      chmin(ans[v], ans[l] + ans[r] + depth[v] + 2);
    }
  }
  cout << ans[0] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const ll INF = LLONG_MAX / 10;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int n;
vector<pii> tree;
vi leaves, nosave, ans, depth;

ll count_leaves(int v) {
  if (v == n - 1)
    return leaves[v] = 1;
  ll res = 0;
  res = count_leaves(tree[v].first) + count_leaves(tree[v].second);
  return leaves[v] = res;
}

void get_depth(int v) {
  int l = tree[v].first, r = tree[v].second;
  if (l != n - 1) {
    depth[l] = depth[v] + 1;
    get_depth(l);
  }
  if (r != n - 1) {
    depth[r] = depth[v] + 1;
    get_depth(r);
  }
}

ll ns(int v) {
  if (v == n - 1)
    return 0;
  ll res = 0;
  res = ns(tree[v].first) + ns(tree[v].second) + leaves[v];
  return nosave[v] = res;
}

ll solve(int v) {
  if (v == n - 1)
    return ans[v] = 0;
  if (ans[v] >= 0)
    return ans[v];
  ll res = INF;
  int cl = tree[v].first, cr = tree[v].second;
  chmin(res, nosave[cl] + leaves[cl] + solve(cr) + 1);
  chmin(res, solve(cl) + nosave[cr] + leaves[cr] + 1);
  chmin(res, solve(cl) + solve(cr) + depth[v] + 2);
  return ans[v] = res;
}

int main() {
  cin.sync_with_stdio(false);
  cout << fixed << setprecision(10);
  cin >> n;
  tree.resize(n);
  REP(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (a == n - 1)
      a = n - 1;
    if (b == n - 1)
      b = n - 1;
    tree[i] = pii(a, b);
  }
  depth.resize(n);
  get_depth(0);
  leaves.resize(n);
  count_leaves(0);
  nosave.resize(n);
  ns(0);
  ans.resize(n, -1);
  solve(0);
  cout << ans[0] << endl;
  return 0;
}