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

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

using namespace std;

#define ll long long
#define ull unsigned long long
#define INF (1 << 30)
#define LLINF (1LL << 62)
#define MOD7 1000000007
#define MOD9 1000000009
#define eps 1e-9
#define P pair<int, int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pul pair<ull, ull>
#define all(a) (a).begin(), (a).end()
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)

void YES() { cout << "YES\\n"; }
void NO() { cout << "NO\\n"; }
void Yes() { cout << "Yes\\n"; }
void No() { cout << "No\\n"; }

template <class T> class Tree {
public:
  class Node {
  public:
    int parent = -1;
    vector<int> children;

    T value;
  };

  vector<Node> data;
  int root = -1;
  int _size = 1;

  Tree() { data.resize(1); }

  Node operator[](int id) { return data[id]; }

  void resize(int size) {
    if (size > _size) {
      while (_size < size) {
        _size *= 2;
      }
      data.resize(_size);
    }
  }

  void addNode(int id, vector<int> &children, T value) {
    resize(id + 1);

    if (root == -1) {
      root = id;
      data[id].parent = -1;
    }

    data[id].children = children;
    data[id].value = value;

    for (int i = 0; i < children.size(); ++i) {
      resize(children[i] + 1);
      data[children[i]].parent = id;
      if (root == children[i]) {
        while (data[root].parent != -1) {
          root = data[root].parent;
        }
      }
    }
  }

  vector<int> getSibling(int id) {
    vector<int> dst;
    if (data[id].parent == -1)
      return dst;

    int parent = data[id].parent;
    for (int i = 0; i < data[parent].children.size(); ++i) {
      if (data[parent].children[i] != id)
        dst.push_back(data[parent].children[i]);
    }

    return dst;
  }

  int getDegree(int id) { return data[id].children.size(); }

  int getDepth(int id) {
    int depth = 0;
    while (data[id].parent != -1) {
      id = data[id].parent;
      depth++;
    }
    return depth;
  }

  int getHeight(int id) {
    int maxh = -1;
    for (int i = 0; i < data[id].children.size(); ++i) {
      maxh = max(maxh, getHeight(data[id].children[i]));
    }
    return maxh + 1;
  }

  string getName(int id) {
    if (data[id].parent == -1)
      return "root";
    if (data[id].children.size() == 0)
      return "leaf";
    return "internal node";
  }
};

int main() {
  Tree<int> tree;
  int n, id, c;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    vector<int> child;
    cin >> id;
    for (int i = 0; i < 2; ++i) {
      cin >> c;
      if (c != -1)
        child.push_back(c);
    }
    tree.addNode(id, child, 0);
  }

  for (int i = 0; i < n; ++i) {
    printf("node %d: parent = %d, ", i, tree[i].parent);
    printf("sibling = ");
    auto v = tree.getSibling(i);
    if (v.size() == 0) {
      printf("-1, ");
    } else {
      printf("%d, ", v[0]);
    }
    printf("degree = %d, ", tree.getDegree(i));
    printf("depth = %d, height = %d, ", tree.getDepth(i), tree.getHeight(i));
    printf("%s\\n", tree.getName(i).c_str());
  }

  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

#define ll long long
#define ull unsigned long long
#define INF (1 << 30)
#define LLINF (1LL << 62)
#define MOD7 1000000007
#define MOD9 1000000009
#define eps 1e-9
#define P pair<int, int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pul pair<ull, ull>
#define all(a) (a).begin(), (a).end()
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)

void YES() { cout << "YES\\n"; }
void NO() { cout << "NO\\n"; }
void Yes() { cout << "Yes\\n"; }
void No() { cout << "No\\n"; }

template <class T> class Tree {
public:
  class Node {
  public:
    int parent = -1;
    vector<int> children;

    T value;
  };

  vector<Node> data;
  int root = -1;
  int _size = 1;

  Tree() { data.resize(1); }

  Node operator[](int id) { return data[id]; }

  void resize(int size) {
    if (size > _size) {
      while (_size < size) {
        _size *= 2;
      }
      data.resize(_size);
    }
  }

  void addNode(int id, vector<int> &children, T value) {
    resize(id + 1);

    if (root == -1) {
      root = id;
      data[id].parent = -1;
    }

    data[id].children = children;
    data[id].value = value;

    for (int i = 0; i < children.size(); ++i) {
      resize(children[i] + 1);
      data[children[i]].parent = id;
      if (root == children[i]) {
        while (data[root].parent != -1) {
          root = data[root].parent;
        }
      }
    }
  }

  vector<int> getSibling(int id) {
    vector<int> dst;
    if (data[id].parent == -1) {
      dst.push_back(-1);
      return dst;
    }

    int parent = data[id].parent;
    for (int i = 0; i < data[parent].children.size(); ++i) {
      if (data[parent].children[i] != id)
        dst.push_back(data[parent].children[i]);
    }

    return dst;
  }

  int getDegree(int id) { return data[id].children.size(); }

  int getDepth(int id) {
    int depth = 0;
    while (data[id].parent != -1) {
      id = data[id].parent;
      depth++;
    }
    return depth;
  }

  int getHeight(int id) {
    int maxh = -1;
    for (int i = 0; i < data[id].children.size(); ++i) {
      maxh = max(maxh, getHeight(data[id].children[i]));
    }
    return maxh + 1;
  }

  string getName(int id) {
    if (data[id].parent == -1)
      return "root";
    if (data[id].children.size() == 0)
      return "leaf";
    return "internal node";
  }
};

int main() {
  Tree<int> tree;
  int n, id, c;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    vector<int> child;
    cin >> id;
    for (int i = 0; i < 2; ++i) {
      cin >> c;
      if (c != -1)
        child.push_back(c);
    }
    tree.addNode(id, child, 0);
  }

  for (int i = 0; i < n; ++i) {
    printf("node %d: parent = %d, ", i, tree[i].parent);
    printf("sibling = %d, degree = %d, ", tree.getSibling(i)[0],
           tree.getDegree(i));
    printf("depth = %d, height = %d, ", tree.getDepth(i), tree.getHeight(i));
    printf("%s\\n", tree.getName(i).c_str());
  }

  return 0;
}