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

#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <vector>

using namespace std;
typedef long long ll;

const int MN = 500500;

int n;
ll d[MN][2];
ll dps[MN], num[MN], dist[MN];

// ll num(int i) {
//     if (i == -1) return 1;
//     return num(d[i][0]) + num(d[i][1]);
// }

// ll dist(int i) {
//     if (i == -1) return 0;
//     return dist(d[i][0]) + num(d[i][0]) + dist(d[i][1]) + num(d[i][1]);
// }

ll dfs(int i) {
  if (i == n - 1)
    return 0;
  ll res = dist[i];
  res = min(res, dist[d[i][0]] + num[d[i][0]] + 1 + dfs(d[i][1]));
  res = min(res, 1 + dfs(d[i][0]) + dist[d[i][1]] + num[d[i][1]]);
  res = min(res, 1 + dfs(d[i][0]) + dps[i] + 1 + dfs(d[i][1]));
  return res;
}

void main2() {
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    a--;
    b--;
    d[i][0] = a;
    d[i][1] = b;
  }
  //    init(0, 0);
  dps[0] = 0;
  for (int i = 0; i < n - 1; i++) {
    dps[d[i][0]] = dps[d[i][1]] = dps[i] + 1;
  }
  num[n - 1] = 1;
  dist[n - 1] = 0;
  for (int i = n - 2; i >= 0; i--) {
    num[i] = num[d[i][0]] + num[d[i][1]];
    dist[i] = num[i] + dist[d[i][0]] + dist[d[i][1]];
  }
  //    for (int i = 0; i < n-1; i++) {
  //        printf("dps:%lld num:%lld dist:%lld dfs:%lld\\n", dps[i], num[i],
  //        dist[i], dfs(i));
  //    }
  printf("%lld\\n", dfs(0));
}

ll eord, enew;
int main() {
  const int SZ = 120 * 1024 * 1024;
  void *p = malloc(SZ);
  enew = (long long)p + SZ - 1;

  __asm__("mov %rsp, eord");
  __asm__("mov enew, %rsp");

  main2();

  __asm__("mov eord, %rsp");
}

Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <vector>

using namespace std;
typedef long long ll;

const int MN = 500500;

int n;
ll d[MN][2];
ll dps[MN], num[MN], dist[MN];

// ll num(int i) {
//     if (i == -1) return 1;
//     return num(d[i][0]) + num(d[i][1]);
// }

// ll dist(int i) {
//     if (i == -1) return 0;
//     return dist(d[i][0]) + num(d[i][0]) + dist(d[i][1]) + num(d[i][1]);
// }

ll dfs(int i) {
  if (i == n - 1)
    return 0;
  ll res = dist[i];
  res = min(res, dist[d[i][0]] + num[d[i][0]] + 1 + dfs(d[i][1]));
  res = min(res, 1 + dfs(d[i][0]) + dist[d[i][1]] + num[d[i][1]]);
  res = min(res, 1 + dfs(d[i][0]) + dps[i] + 1 + dfs(d[i][1]));
  return res;
}

void main2() {
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    a--;
    b--;
    d[i][0] = a;
    d[i][1] = b;
  }
  //    init(0, 0);
  dps[0] = 0;
  for (int i = 0; i < n - 1; i++) {
    dps[d[i][0]] = dps[d[i][1]] = dps[i] + 1;
  }
  num[n - 1] = 1;
  dist[n - 1] = 0;
  for (int i = n - 2; i >= 0; i--) {
    num[i] = num[d[i][0]] + num[d[i][1]];
    dist[i] = num[i] + dist[d[i][0]] + dist[d[i][1]];
  }
  //    for (int i = 0; i < n-1; i++) {
  //        printf("dps:%lld num:%lld dist:%lld dfs:%lld\\n", dps[i], num[i],
  //        dist[i], dfs(i));
  //    }
  printf("%lld\\n", dfs(0));
}

ll eord, enew;
int main() {
  const int SZ = 120 * 1024 * 1024;
  void *p = malloc(SZ);
  enew = (long long)p + SZ - 1;

  //    __asm__("mov %rsp, eord");
  //    __asm__("mov enew, %rsp");

  main2();

  //    __asm__("mov eord, %rsp");
}