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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1},
          dy[] = {0, -1, 0, 1, 1, -1, -1, 1};

int N;
std::vector<vector<int>> G;
std::vector<int> parent;
std::vector<int> depth;
std::vector<int> type;
std::vector<int> sibling;
std::vector<int> degree;
std::vector<int> height;

int dfs(int i, int d) {
  int h = 0;

  depth[i] = d;
  int deg = 0;

  for (int j = 0; j < G[i].size(); j++) {

    if (G[i][j] != -1) {
      h = max(h, dfs(G[i][j], d + 1) + 1);
      parent[G[i][j]] = i;
      deg++;
      sibling[G[i][j]] = G[i][1 - j];
    }
  }

  degree[i] = deg;

  if (G[i][0] == -1 && G[i][1] == -1) {
    type[i] = 0;
    height[i] = 0;
    return 0;
  } else {
    type[i] = 1;
    return height[i] = h;
  }
}

int main() {

  cin >> N;
  for (int i = 0; i < N; i++) {
    G.push_back(vector<int>());
    parent.push_back(-1);
    depth.push_back(0);
    type.push_back(0);
    sibling.push_back(-1);
    degree.push_back(-1);
    height.push_back(0);
  }
  std::vector<int> notroot(N, 0);

  for (int i = 0; i < N; i++) {
    int id;
    cin >> id;
    for (int j = 0; j < 2; j++) {
      int child;
      cin >> child;

      if (child != -1)
        notroot[child] = 1;
      G[id].push_back(child);
    }
  }

  int root;
  for (int i = 0; i < N; i++) {
    if (notroot[i] == 0) {
      root = i;
      break;
    }
  }

  dfs(root, 0);

  for (int i = 0; i < N; i++) {
    cout << "node " << i << ": ";
    cout << "parent = " << parent[i] << ", ";
    cout << "sibling = " << sibling[i] << ", ";
    cout << "degree = " << degree[i] << ", ";
    cout << "depth = " << depth[i] << ", ";
    cout << "height = " << height[i] << ", ";
    if (depth[i] == 0) {
      cout << "root";
    } else {
      if (type[i] == 0) {
        cout << "leaf";
      } else {
        cout << "internal node";
      }
    }
    cout << endl;
  }

  return 0;
}

Error: -6
free(): invalid pointer


Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1},
          dy[] = {0, -1, 0, 1, 1, -1, -1, 1};

int N;
std::vector<vector<int>> G;
std::vector<int> parent;
std::vector<int> depth;
std::vector<int> type;
std::vector<int> sibling;
std::vector<int> degree;
std::vector<int> height;

int dfs(int i, int d) {
  cout << i << endl;
  depth[i] = d;
  int deg = 0;
  int h = 0;
  for (int j = 0; j < G[i].size(); j++) {
    if (G[i][j] != -1) {
      h = max(h, dfs(G[i][j], d + 1) + 1);
      parent[G[i][j]] = i;
      deg++;
    }
    sibling[G[i][j]] = G[i][1 - j];
  }
  degree[i] = deg;

  if (G[i][0] == -1 && G[i][1] == -1) {
    type[i] = 0;
    height[i] = 0;
    return 0;
  } else {
    type[i] = 1;
    return height[i] = h;
  }
}

int main() {

  cin >> N;
  for (int i = 0; i < N; i++) {
    G.push_back(vector<int>());
    parent.push_back(-1);
    depth.push_back(0);
    type.push_back(0);
    sibling.push_back(-1);
    degree.push_back(-1);
    height.push_back(0);
  }
  std::vector<int> notroot(N, 0);

  for (int i = 0; i < N; i++) {
    int id;
    cin >> id;
    for (int j = 0; j < 2; j++) {
      int child;
      cin >> child;

      if (child != -1)
        notroot[child] = 1;
      G[id].push_back(child);
    }
  }
  int root;
  for (int i = 0; i < N; i++) {
    if (notroot[i] == 0) {
      root = i;
      break;
    }
  }
  dfs(root, 0);
  for (int i = 0; i < N; i++) {
    cout << "node " << i << ": ";
    cout << "parent = " << parent[i] << ", ";
    cout << "sibling = " << sibling[i] << ", ";
    cout << "degree = " << degree[i] << ", ";
    cout << "depth = " << depth[i] << ", ";
    cout << "height = " << height[i] << ", ";
    if (depth[i] == 0) {
      cout << "root";
    } else {
      if (type[i] == 0) {
        cout << "leaf";
      } else {
        cout << "internal node";
      }
    }
    cout << endl;
  }

  return 0;
}