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

#include <stdio.h>
#include <vector>

using namespace std;

struct Node {
  Node() {
    parent = -1;
    depth = num_of_children = 0;
  }
  int parent;
  int depth;
  vector<int> children;
  int num_of_children;
};

void calcDepth(Node nodes[], int root_id) {
  for (int i = 0; i < nodes[root_id].num_of_children; i++) {
    nodes[nodes[root_id].children[i]].depth = nodes[root_id].depth + 1;
    calcDepth(nodes, nodes[root_id].children[i]);
  }
}

int main() {
  int n, id, k, child_id, root_id;
  scanf("%d", &n);
  Node nodes[n];

  for (int i = 0; i < n; i++) {
    scanf("%d %d", &id, &k);
    for (int j = 0; j < k; j++) {
      scanf("%d", &child_id);
      nodes[id].children.push_back(child_id);
      nodes[id].num_of_children++;
      nodes[child_id].parent = id;
    }
  }

  for (root_id = 0; root_id < n; root_id++) {
    if (nodes[root_id].parent == -1)
      break;
  }

  calcDepth(nodes, root_id);

  for (int i = 0; i < n; i++) {
    if (nodes[i].parent == -1) {
      printf("node %d: parent = -1, depth = 0, root, ", i);
    } else if (nodes[i].num_of_children == 0) {
      printf("node %d: parent = %d, depth = %d, leaf, ", i, nodes[i].parent,
             nodes[i].depth);
    } else {
      printf("node %d: parent = %d, depth = %d, internal node, ", i,
             nodes[i].parent, nodes[i].depth);
    }
    printf("[");
    for (int j = 0; j < nodes[i].num_of_children - 1; j++) {
      printf("%d, ", nodes[i].children[j]);
    }
    if (nodes[i].num_of_children > 0) {
      printf("%d", nodes[i].children[nodes[i].num_of_children - 1]);
    }
    printf("]\\n");
  }
}


Error: WA

Fixed submission:

#include <cstdio>
#include <iostream>
#include <vector>

struct Node {
  int parent;
  int depth;
  int num_of_child;
  std::vector<int> children;
  void NewNode(void) {
    parent = -1;
    depth = num_of_child = 0;
  }
};

void calcHeight(std::vector<struct Node> &node, int root_id) {
  for (int i = 0; i < node[root_id].num_of_child; i++) {
    node[node[root_id].children[i]].depth = node[root_id].depth + 1;
    calcHeight(node, node[root_id].children[i]);
  }
}

int GetRootID(std::vector<struct Node> &node) {
  for (int i = 0; i < node.size(); i++)
    if (node[i].parent == -1)
      return i;
}

int main(void) {
  int n, k, id;
  std::cin >> n;
  std::vector<struct Node> node(n);
  for (int i = 0; i < n; i++) {
    std::cin >> id >> k;
    node[id].num_of_child = k;
    for (int j = 0; j < k; j++) {
      int child_id;
      std::cin >> child_id;
      node[id].children.push_back(child_id);
      node[child_id].parent = id;
    }
  }
  int root_id = GetRootID(node);
  calcHeight(node, root_id);

  for (int i = 0; i < n; i++) {
    if (node[i].parent == -1) {
      printf("node %d: parent = -1, depth = 0, root, ", i);
    } else if (node[i].num_of_child == 0) {
      printf("node %d: parent = %d, depth = %d, leaf, ", i, node[i].parent,
             node[i].depth);
    } else {
      printf("node %d: parent = %d, depth = %d, internal node, ", i,
             node[i].parent, node[i].depth);
    }
    printf("[");
    for (int j = 0; j < node[i].num_of_child - 1; j++) {
      printf("%d, ", node[i].children[j]);
    }
    if (node[i].num_of_child > 0) {
      printf("%d", node[i].children[node[i].num_of_child - 1]);
    }
    printf("]\\n");
  }
  return 0;
}