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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>

#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it, X)                                                         \\
  for (__typeof((X).begin()) it = (X).begin(); it != (X).end(); it++)
#define ite iterator
#define mp make_pair
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define pb push_back
#define sec second
#define sz(x) ((int)(x).size())

using namespace std;

struct timer {
  time_t start;
  timer() { start = clock(); }
  ~timer() {
    cerr << 1. * (clock() - start) / CLOCKS_PER_SEC << " secs" << endl;
  }
};

typedef istringstream iss;
typedef long long ll;
typedef pair<int, int> pi;
typedef stringstream sst;
typedef vector<int> vi;

int N, y[500010], n[500010];
ll end[500010], nosave[500010], dep[500010], ans[500010];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);

  cin >> N;
  rep(i, N - 1) cin >> y[i] >> n[i], y[i]--, n[i]--;

  end[N - 1] = 1;
  for (int i = N - 2; i >= 0; i--) {
    end[i] = end[y[i]] + end[n[i]];
  }

  nosave[N - 1] = 0;
  for (int i = N - 2; i >= 0; i--) {
    nosave[i] = end[y[i]] + nosave[y[i]] + end[n[i]] + nosave[n[i]];
  }

  dep[0] = 0;
  rep(i, N - 1) { dep[y[i]] = dep[n[i]] = dep[i] + 1; }

  ans[N - 1] = 0;
  for (int i = N - 2; i >= 0; i--) {
    ans[i] = min(1 + ans[y[i]] + dep[i] + 1 + ans[n[i]],
                 min(end[y[i]] + nosave[y[i]] + 1 + ans[n[i]],
                     end[n[i]] + nosave[n[i]] + 1 + ans[y[i]]));
  }
  cout << ans[0] << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>

#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it, X)                                                         \\
  for (__typeof((X).begin()) it = (X).begin(); it != (X).end(); it++)
#define ite iterator
#define mp make_pair
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define pb push_back
#define sec second
#define sz(x) ((int)(x).size())

using namespace std;

struct timer {
  time_t start;
  timer() { start = clock(); }
  ~timer() {
    cerr << 1. * (clock() - start) / CLOCKS_PER_SEC << " secs" << endl;
  }
};

typedef istringstream iss;
typedef long long ll;
typedef pair<int, int> pi;
typedef stringstream sst;
typedef vector<int> vi;

int N, y[500010], n[500010];
ll memo1[500010], memo2[500010], memo3[500010];

ll end(int cur) {
  if (memo1[cur] != -1)
    return memo1[cur];
  ll &res = memo1[cur];
  if (cur == N - 1)
    return res = 1;
  return res = end(y[cur]) + end(n[cur]);
}

ll nosave(int cur) {
  if (memo2[cur] != -1)
    return memo2[cur];
  ll &res = memo2[cur];
  if (cur == N - 1)
    return res = 0;
  return res = memo1[cur] + nosave(y[cur]) +
               nosave(n[cur]); // end(y[cur])+nosave(y[cur]) +
                               // end(n[cur])+nosave(n[cur]);
}

ll f(int cur, int dep) {
  if (memo3[cur] != -1)
    return memo3[cur];
  ll &res = memo3[cur];
  if (cur == N - 1)
    return res = 0;
  return res = min(
             1 + f(y[cur], dep + 1) + dep + 1 + f(n[cur], dep + 1),
             min(memo1[y[cur]] + memo2[y[cur]] /*end(y[cur])+nosave(y[cur])*/ +
                     1 + f(n[cur], dep + 1),
                 memo1[n[cur]] + memo2[n[cur]] /*end(n[cur])+nosave(n[cur])*/ +
                     1 + f(y[cur], dep + 1)));
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);

  cin >> N;
  rep(i, N - 1) cin >> y[i] >> n[i], y[i]--, n[i]--;
  memset(memo1, -1, sizeof(memo1));
  memset(memo2, -1, sizeof(memo2));
  memset(memo3, -1, sizeof(memo3));
  end(0);
  nosave(0);
  cout << f(0, 0) << endl;
}