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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF = 1e9;

struct Node_t {
  Node_t() {
    id_ = left_ = right_ = parent_ = depth_ = height_ = -1;
    deg_ = 0;
    type_ = -1;
    sibling_ = -1;
  }

  int id_, left_, right_, parent_, depth_, height_, deg_, type_;
  int sibling_;
};

const char *types[] = {"root", "internal node", "leaf"};

void print_node(Node_t &node) {
  fprintf(stdout,
          "node %d: parent = %d, sibling = %d, degree = %d, depth = %d, height "
          "= %d, %s\\n",
          node.id_, node.parent_, node.sibling_, node.deg_, node.depth_,
          node.height_, types[node.type_]);
}

void set_depth(vector<Node_t> &nodes, int id, int depth) {
  nodes[id].depth_ = depth;
  int l = nodes[id].left_, r = nodes[id].right_;
  if (l != -1) {
    set_depth(nodes, l, depth + 1);
  }
  if (r != -1) {
    set_depth(nodes, r, depth + 1);
  }
  return;
}

int set_height(vector<Node_t> &nodes, int id) {
  int l = nodes[id].left_, r = nodes[id].right_;
  if (l == -1 && r == -1) {
    nodes[id].height_ = 0;
  } else if (l == -1) {
    nodes[id].height_ = 1 + set_height(nodes, r);
  } else if (r == -1) {
    nodes[id].height_ = 1 + set_height(nodes, l);
  } else {
    nodes[id].height_ = 1 + max(set_height(nodes, l), set_height(nodes, r));
  }
  return nodes[id].height_;
}

void set_sibling(vector<Node_t> &nodes) {
  int n = nodes.size();
  for (int i = 0; i < n; ++i) {
    if (nodes[i].deg_ == 2) {
      nodes[nodes[i].left_].sibling_ = nodes[i].right_;
      nodes[nodes[i].right_].sibling_ = nodes[i].left_;
    }
  }
}

int main() {
  int n = 0;
  fscanf(stdin, "%d", &n);
  vector<Node_t> nodes(n);
  for (int i = 0; i < n; ++i) {
    int id, l, r, deg;
    fscanf(stdin, "%d %d %d", &id, &l, &r);
    nodes[id].id_ = id;
    nodes[id].left_ = l;
    nodes[id].right_ = r;
    if (l != -1) {
      nodes[l].parent_ = id;
    }
    if (r != -1) {
      nodes[r].parent_ = id;
    }
    deg = (l != -1) + (r != -1);
    nodes[id].deg_ = deg;
  }
  int root_id = -1;
  for (int i = 0; i < n; ++i) {
    if (nodes[i].parent_ == -1) {
      root_id = i;
      break;
    }
  }
  for (int i = 0; i < n; ++i) {
    if (i == root_id) {
      continue;
    }
    if (nodes[i].deg_ == 0) {
      nodes[i].type_ = 2;
    } else {
      nodes[i].type_ = 1;
    }
  }
  nodes[root_id].type_ = 0;
  set_depth(nodes, root_id, 0);
  set_height(nodes, root_id);
  set_sibling(nodes);
  for (int i = 0; i < n; ++i) {
    print_node(nodes[i]);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF = 1e9;

struct Node_t {
  Node_t() {
    id_ = left_ = right_ = parent_ = depth_ = height_ = -1;
    deg_ = 0;
    type_ = -1;
    sibling_ = -1;
  }

  int id_, left_, right_, parent_, depth_, height_, deg_, type_;
  int sibling_;
};

const char *types[] = {"root", "internal node", "leaf"};

void print_node(Node_t &node) {
  fprintf(stdout,
          "node: %d: parent = %d, sibling = %d, degree = %d, depth = %d, "
          "height = %d, %s\\n",
          node.id_, node.parent_, node.sibling_, node.deg_, node.depth_,
          node.height_, types[node.type_]);
}

void set_depth(vector<Node_t> &nodes, int id, int depth) {
  nodes[id].depth_ = depth;
  int l = nodes[id].left_, r = nodes[id].right_;
  if (l != -1) {
    set_depth(nodes, l, depth + 1);
  }
  if (r != -1) {
    set_depth(nodes, r, depth + 1);
  }
  return;
}

int set_height(vector<Node_t> &nodes, int id) {
  int l = nodes[id].left_, r = nodes[id].right_;
  if (l == -1 && r == -1) {
    nodes[id].height_ = 0;
  } else if (l == -1) {
    nodes[id].height_ = 1 + set_height(nodes, r);
  } else if (r == -1) {
    nodes[id].height_ = 1 + set_height(nodes, l);
  } else {
    nodes[id].height_ = 1 + max(set_height(nodes, l), set_height(nodes, r));
  }
  return nodes[id].height_;
}

void set_sibling(vector<Node_t> &nodes) {
  int n = nodes.size();
  for (int i = 0; i < n; ++i) {
    if (nodes[i].deg_ == 2) {
      nodes[nodes[i].left_].sibling_ = nodes[i].right_;
      nodes[nodes[i].right_].sibling_ = nodes[i].left_;
    }
  }
}

int main() {
  int n = 0;
  fscanf(stdin, "%d", &n);
  vector<Node_t> nodes(n);
  for (int i = 0; i < n; ++i) {
    int id, l, r, deg;
    fscanf(stdin, "%d %d %d", &id, &l, &r);
    nodes[id].id_ = id;
    nodes[id].left_ = l;
    nodes[id].right_ = r;
    nodes[l].parent_ = nodes[r].parent_ = id;
    // deg = (l != -1) + (r != -1);
    // nodes[id].deg_ = deg;
  }
  int root_id = -1;
  // for (int i = 0; i < n; ++i) {
  //   if (nodes[i].parent_ == -1) {
  //     root_id = i;
  //     break;
  //   }
  // }
  // for (int i = 0; i < n; ++i) {
  //   if (i == root_id) { continue; }
  //   if (nodes[i].deg_ == 0) { nodes[i].type_ = 2; }
  //   else { nodes[i].type_ = 1; }
  // }
  // nodes[root_id].type_ = 0;
  // set_depth(nodes, root_id, 0);
  // set_height(nodes, root_id);
  // set_sibling(nodes);
  // for (int i = 0; i < n; ++i) {
  //   print_node(nodes[i]);
  // }
  return 0;
}