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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
const int nmax = 100;

int root;
vector<int> dep(nmax), par(nmax), high(nmax);
vector<int> des[nmax];

void dep_dfs(int id) {
  for (int i = 0; i < (int)des[id].size(); i++) {
    dep[des[id][i]] = dep[id] + 1;
    dep_dfs(des[id][i]);
  }
}
void hi_dfs(int id) {
  if (par[id] != -1) {
    high[par[id]] = max(high[par[id]], high[id] + 1);
    hi_dfs(par[id]);
  }
}
int bro(int id) {
  int p = par[id];
  if (p == -1)
    return -1;
  for (int i = 0; i < (int)des[p].size(); i++)
    if (des[p][i] != id)
      return des[p][i];
  return -1;
}

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

  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    par[i] = -1;
    high[i] = 0;
    dep[i] = 0;
  }
  // cout<<0<<endl;
  for (int i = 0; i < n; i++) {
    int id, left, right;
    cin >> id >> left >> right;
    if (left != -1) {
      des[id].push_back(left);
      par[left] = id;
    }
    if (right != -1) {
      des[id].push_back(right);
      par[right] = id;
    }
    // cout<<i<<endl;
  }
  for (int i = 0; i < n; i++) {
    if (par[i] == -1) {
      root = i;
      dep_dfs(root);
    }
    if ((int)des[i].size() == 0) {
      high[i] = 0;
      hi_dfs(i);
    }
    // cout<<i<<endl;
  }
  for (int i = 0; i < n; i++) {
    string type;
    if (par[i] == -1)
      type = "root";
    else if ((int)des[i].size() == 0)
      type = "leaf";
    else
      type = "internal node";
    cout << "node"
         << " " << i << ": parent = " << par[i] << ", sibling = " << bro(i)
         << ", degree = " << des[i].size() << ", depth = " << dep[i]
         << ", height = " << high[i] << ", " << type << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
const int nmax = 9;

int root;
vector<int> dep(nmax), par(nmax), high(nmax);
vector<int> des[nmax];

void dep_dfs(int id) {
  for (int i = 0; i < (int)des[id].size(); i++) {
    dep[des[id][i]] = dep[id] + 1;
    dep_dfs(des[id][i]);
  }
}
void hi_dfs(int id) {
  if (par[id] != -1) {
    high[par[id]] = max(high[par[id]], high[id] + 1);
    hi_dfs(par[id]);
  }
}
int bro(int id) {
  int p = par[id];
  if (p == -1)
    return -1;
  for (int i = 0; i < (int)des[p].size(); i++)
    if (des[p][i] != id)
      return des[p][i];
  return -1;
}

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

  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    par[i] = -1;
    high[i] = 0;
    dep[i] = 0;
  }
  // cout<<0<<endl;
  for (int i = 0; i < n; i++) {
    int id, left, right;
    cin >> id >> left >> right;
    if (left != -1) {
      des[id].push_back(left);
      par[left] = id;
    }
    if (right != -1) {
      des[id].push_back(right);
      par[right] = id;
    }
    // cout<<i<<endl;
  }
  for (int i = 0; i < n; i++) {
    if (par[i] == -1) {
      root = i;
      dep_dfs(root);
    }
    if ((int)des[i].size() == 0) {
      high[i] = 0;
      hi_dfs(i);
    }
    // cout<<i<<endl;
  }
  for (int i = 0; i < n; i++) {
    string type;
    if (par[i] == -1)
      type = "root";
    else if ((int)des[i].size() == 0)
      type = "leaf";
    else
      type = "internal node";
    cout << "node"
         << " " << i << ": parent = " << par[i] << ", sibling = " << bro(i)
         << ", degree = " << des[i].size() << ", depth = " << dep[i]
         << ", height = " << high[i] << ", " << type << endl;
  }
  return 0;
}