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

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

int p[25], sib[26], lft[26], rht[26], deg[26], dep[26], hei[26];
string type[26];
int n;

void Search(int, int); // now,prev
void PrintNode();

int main(int argc, char *argv[]) {
  int id, left, right, cnt, rnode;
  bool flag[26];
  memset(flag, 0, sizeof(flag));

  cin >> n;

  for (int i = 0; i < n; i++) {
    p[i] = -1;
    sib[i] = -1;
    lft[i] = -1;
    rht[i] = -1;
    deg[i] = 0;
    dep[i] = 0;
    hei[i] = 0;
  }

  for (int i = 0; i < n; i++) {
    cin >> id >> left >> right;
    cnt = 0;

    if (left != -1) {
      cnt++;
      flag[left] = true;
      lft[id] = left;
      if (right != -1)
        sib[left] = right;
    }
    if (right != -1) {
      cnt++;
      flag[right] = true;
      rht[id] = right;
      if (left != -1)
        sib[right] = left;
    }

    deg[id] = cnt;
  }

  for (int i = 0; i < n; i++)
    if (!flag[i]) {
      rnode = i;
      break;
    }

  // 探索
  Search(rnode, -1);
  type[rnode] = "root";

  PrintNode();

  return 0;
}

void Search(int now, int prev) {
  int l, r;
  l = r = -1;

  if (prev != -1) {
    p[now] = prev;
    dep[now] = dep[prev] + 1;
  }

  if (lft[now] == -1 && rht[now] == -1)
    type[now] = "leaf";
  else {
    type[now] = "internal node";
    if (lft[now] != -1) {
      Search(lft[now], now);
      l = hei[lft[now]];
    }
    if (rht[now] != -1) {
      Search(rht[now], now);
      r = hei[rht[now]];
    }
  }

  hei[now] = max(l, r) + 1;
}

void PrintNode() {
  for (int i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << p[i] << ", sibling = " << sib[i]
         << ", degree = " << deg[i];
    cout << ", depth = " << dep[i] << ", height = " << hei[i] << ", " << type[i]
         << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

typedef struct node Node;

struct node {
  int id, p, sib, deg, dep, hei;
  int clft, crht;
  string type;
  node() {
    p = -1;
    sib = -1;
    deg = -1;
    dep = -1;
    hei = -1;
    clft = -1;
    crht = -1;
  }
};

vector<Node> nodes(25);
int n;

int Calc(int, int);
void PrintNode();

int main(int argc, char *argv[]) {
  int id, lft, rht;
  bool flag[25];
  int rootnode = -1, cnt;
  memset(flag, 0, sizeof(flag));

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> id >> lft >> rht;
    nodes[id].id = id;
    cnt = 0;
    if (lft != -1) {
      cnt++;
      nodes[id].clft = lft;
      flag[lft] = true;
      if (rht != -1)
        nodes[lft].sib = rht;
    }
    if (rht != -1) {
      cnt++;
      nodes[id].crht = rht;
      flag[rht] = true;
      if (lft != -1)
        nodes[rht].sib = lft;
    }
    nodes[id].deg = cnt;
  }

  for (int i = 0; i < n && rootnode == -1; i++) {
    if (!flag[i])
      rootnode = i;
  }

  nodes[rootnode].hei = Calc(rootnode, 0);
  nodes[rootnode].type = "root";
  PrintNode();

  return 0;
}

int Calc(int nownode, int dep) {
  Node *tmp = &nodes[nownode];
  int cnt = 0;

  tmp->dep = dep;
  if (tmp->clft != -1) {
    nodes[tmp->clft].p = nownode;
    cnt++;
  }
  if (tmp->clft != -1) {
    nodes[tmp->crht].p = nownode;
    cnt++;
  }

  if (cnt == 0) {
    tmp->type = "leaf";
    tmp->hei = 0;
  } else {
    if (dep != 0)
      tmp->type = "internal node";
    tmp->hei = max(Calc(tmp->clft, dep + 1), Calc(tmp->crht, dep + 1)) + 1;
  }
  return tmp->hei;
}

void PrintNode() {
  Node *tmp;

  for (int i = 0; i < n; i++) {
    tmp = &nodes[i];

    cout << "node " << tmp->id << ": parent = " << tmp->p
         << ", sibling = " << tmp->sib;
    cout << ", degree = " << tmp->deg << ", depth = " << tmp->dep
         << ", height = " << tmp->hei << ", " << tmp->type << endl;
  }
}