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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N;
#define INF (1 << 30)
struct node {
  int y, n;
  ll c, l, e, h; // number of child_node, number of leaf, number of pass
                 // edge,number of high
  node() {
    y = -1;
    n = -1;
    c = 0;
    l = 1;
    e = 0;
    h = INF;
  }
};
node v[500001];
ll dp[500001];
ll solve() {
  dp[N - 1] = 0LL;
  fill(dp, dp + N, 1000000000000000000LL);
  for (int id = N - 2; id >= 0; id--) {
    ll ret = 0;
    int to1 = v[id].y, to2 = v[id].n;

    ll ncr1 = dp[to1] + 1LL;
    ll ncr2 = v[to1].e + v[to1].l;

    ll ncl1 = dp[to2] + 1LL;
    ll ncl2 = v[to2].e + v[to2].l;

    ret = min(ncr1 + ncl1 + v[id].h,
              min(ncr1 + ncl2, min(ncr2 + ncl1, ncr2 + ncl2)));
    //  cout << id << " "<< ret << endl;
    // cout << to1 << " "<<  ncr1 << " "<<  ncr2   << endl;
    // cout << to2 << " " <<  ncl1 << " " << ncl2  << endl;
    dp[id] = min(dp[id], ret);
  }
  return dp[0];
}

int main() {
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    cin >> v[i].y >> v[i].n;
    v[i].y--;
    v[i].n--;
  }
  for (int i = N - 2; i >= 0; i--) {
    v[i].c = v[v[i].y].c + v[v[i].n].c + 2LL;
    v[i].l = v[v[i].y].l + v[v[i].n].l;
    v[i].e = v[i].l + v[v[i].y].e + v[v[i].n].e;
  }
  v[0].h = 0LL;
  for (int i = 0; i < N; i++) {
    int a = v[i].y, b = v[i].n;
    if (i < N - 1) {
      v[a].h = min(v[a].h, v[i].h + 1LL);
      v[b].h = min(v[b].h, v[i].h + 1LL);
    }
    //  cout << i << " " << v[i].c << " "<< v[i].l << " "<< v[i].e << " "<<
    //  v[i].h << endl;
  }
  cout << solve() << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N;
#define INF (1 << 30)
struct node {
  int y, n;
  ll c, l, e, h; // number of child_node, number of leaf, number of pass
                 // edge,number of high
  node() {
    y = -1;
    n = -1;
    c = 0;
    l = 1;
    e = 0;
    h = INF;
  }
};
node v[500001];
ll dp[500001];
ll solve(int id) {
  if (dp[id] != -1)
    return dp[id];
  if (id == N - 1)
    return dp[id] = 0LL;
  ll ret = 0;
  int to1 = v[id].y, to2 = v[id].n;

  ll ncr1 = solve(to1) + 1LL;
  ll ncr2 = v[to1].e + v[to1].l;

  ll ncl1 = solve(to2) + 1LL;
  ll ncl2 = v[to2].e + v[to2].l;

  ret = min(ncr1 + ncl1 + v[id].h,
            min(ncr1 + ncl2, min(ncr2 + ncl1, ncr2 + ncl2)));
  //  cout << id << " "<< ret << endl;
  // cout << to1 << " "<<  ncr1 << " "<<  ncr2   << endl;
  // cout << to2 << " " <<  ncl1 << " " << ncl2  << endl;
  return dp[id] = ret;
}

int main() {
  memset(dp, -1, sizeof(dp));
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    cin >> v[i].y >> v[i].n;
    v[i].y--;
    v[i].n--;
  }
  for (int i = N - 2; i >= 0; i--) {
    v[i].c = v[v[i].y].c + v[v[i].n].c + 2LL;
    v[i].l = v[v[i].y].l + v[v[i].n].l;
    v[i].e = v[i].l + v[v[i].y].e + v[v[i].n].e;
  }
  v[0].h = 0LL;
  for (int i = 0; i < N; i++) {
    int a = v[i].y, b = v[i].n;
    if (i < N - 1) {
      v[a].h = min(v[a].h, v[i].h + 1LL);
      v[b].h = min(v[b].h, v[i].h + 1LL);
    }
    //  cout << i << " " << v[i].c << " "<< v[i].l << " "<< v[i].e << " "<<
    //  v[i].h << endl;
  }
  cout << solve(0) << endl;
}