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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define INT_MAX 2147483647
#define Loop(i, n) for (int i = 0; i < (int)n; i++)
#define Loop1(i, n) for (int i = 1; i < (int)n; i++)
#define Loopr(i, n) for (int i = (int)n - 1; i >= 0; i--)
using namespace std;
typedef long long int ll;

struct node {
  int parent, left, right;
};

int n;
vector<node> nodes(25, {-1, -1, -1});

int sib_of(int a) {
  if (nodes[a].parent == -1)
    return -1;
  else {
    int x = nodes[nodes[a].parent].left;
    int y = nodes[nodes[a].parent].right;
    if (x == a)
      return y;
    else
      return x;
  }
}

int deg_of(int a) {
  int ret = 0;
  if (nodes[a].left != -1)
    ret++;
  if (nodes[a].right != -1)
    ret++;
  return ret;
}

int depth_of(int a) {
  if (nodes[a].parent == -1)
    return 0;
  else
    return depth_of(nodes[a].parent) + 1;
}

int height_of(int a) {
  if (a == -1)
    return -1;
  else if (deg_of(a) == 0)
    return 0;
  else
    return max(height_of(nodes[a].left) + 1, height_of(nodes[a].right) + 1);
}

string type_of(int a) {
  if (nodes[a].parent == -1)
    return "root";
  else if (deg_of(a) == 0)
    return "leaf";
  else
    return "internal node";
}

void print(int a) {
  cout << "node " << a << ": parent = " << nodes[a].parent;
  cout << ", sibling = " << sib_of(a);
  cout << ", degree = " << deg_of(a);
  cout << ", depth = " << depth_of(a);
  cout << ", height = " << height_of(a);
  cout << ", " << type_of(a) << endl;
}

int main() {
  cin >> n;
  Loop(i, n) {
    int id;
    cin >> id;
    int l, r;
    cin >> l >> r;
    nodes[id].left = l;
    nodes[id].right = r;
    if (l != -1)
      nodes[l].parent = id;
    if (r != -1)
      nodes[r].parent = id;
  }
  Loop(i, n) { print(i); }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define INT_MAX 2147483647
#define Loop(i, n) for (int i = 0; i < (int)n; i++)
#define Loop1(i, n) for (int i = 1; i < (int)n; i++)
#define Loopr(i, n) for (int i = (int)n - 1; i >= 0; i--)
using namespace std;
typedef long long int ll;

struct node {
  int parent, left, right;
};

int n;
vector<node> nodes(25, {-1, -1, -1});

int sib_of(int a) {
  if (nodes[a].parent == -1)
    return -1;
  else {
    int x = nodes[nodes[a].parent].left;
    int y = nodes[nodes[a].parent].right;
    if (x == a)
      return y;
    else
      return x;
  }
}

int deg_of(int a) {
  int ret = 0;
  if (nodes[a].left != -1)
    ret++;
  if (nodes[a].right != -1)
    ret++;
  return ret;
}

int depth_of(int a) {
  if (nodes[a].parent == -1)
    return 0;
  else
    return depth_of(nodes[a].parent) + 1;
}

int height_of(int a) {
  if (deg_of(a) == 0)
    return 0;
  else
    return max(height_of(nodes[a].left) + 1, height_of(nodes[a].right) + 1);
}

string type_of(int a) {
  if (nodes[a].parent == -1)
    return "root";
  else if (deg_of(a) == 0)
    return "leaf";
  else
    return "internal node";
}

void print(int a) {
  cout << "node " << a << ": parent = " << nodes[a].parent;
  cout << ", sibling = " << sib_of(a);
  cout << ", degree = " << deg_of(a);
  cout << ", depth = " << depth_of(a);
  cout << ", height = " << height_of(a);
  cout << ", " << type_of(a) << endl;
}

int main() {
  cin >> n;
  Loop(i, n) {
    int id;
    cin >> id;
    int l, r;
    cin >> l >> r;
    nodes[id].left = l;
    nodes[id].right = r;
    if (l != -1)
      nodes[l].parent = id;
    if (r != -1)
      nodes[r].parent = id;
  }
  Loop(i, n) { print(i); }
  return 0;
}