alexjercan/bugnet huggingface dataset:
Problem p02279 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
#define MAX 100005
#define NIL -1

//?????????????????¨??????
// left-child right-sibling representation
struct node {
  int parent;
  int left;
  int right;
};

node T[MAX];
int depth[MAX];
int n;

void print(int u) {
  cout << "node " << u << ": ";
  cout << "parent = " << T[u].parent << ", ";
  cout << "depth = " << depth[u] << ", ";

  if (T[u].parent == NIL)
    cout << "root, ";
  else if (T[u].left == NIL)
    cout << "leaf, ";
  else
    cout << "internal node, ";

  cout << "[";

  for (int i = 0, c = T[u].left; c != NIL; i++, c = T[c].right) {
    if (i)
      cout << ", ";
    cout << c;
  }
  cout << "]" << endl;
}

// ????????°?±???±???
void rec(int u, int p) {
  depth[u] = p;
  //?????§???????????????????????±???
  if (T[u].right != NIL)
    rec(T[u].right, p);
  //????????§??????????????±?????????????????±?????±???+1
  if (T[u].left != NIL)
    rec(T[u].left, p + 1);
}

int main() {
  int d;
  int v;
  int c;
  int l;
  int r;
  cin >> n;
  for (int i = 0; i < n; i++)
    T[i].parent = T[i].left = T[i].right = NIL;
  for (int i = 0; i < n; i++) {
    cin >> v >> d;
    for (int j = 0; j < d; j++) {
      cin >> c;
      if (j == 0)
        T[v].left = c;
      else
        T[l].right = c;
      l = c;
      T[c].parent = v;
    }
  }
  for (int i = 0; i < n; i++)
    if (T[i].parent == NIL)
      r = i;
  rec(r, 0);
  for (int i = 0; i < n; i++)
    print(i);
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;
#define MAX 100005
#define NIL -1

//?????????????????¨??????
// left-child right-sibling representation
struct node {
  int parent;
  int leaf;
  int right;
};

node T[MAX];
int depth[MAX];
int n;

void print(int u) {
  int i;
  int c;
  cout << "node" << u << ": ";
  cout << "parent = " << T[u].parent << ", ";
  cout << "depth = " << depth[u] << ", ";

  if (T[u].parent == NIL)
    cout << "root, ";
  else if (T[u].leaf == NIL)
    cout << "leaf, ";
  else
    cout << "internal node, ";

  cout << "[";

  for (i = 0, c = T[u].leaf; c != NIL; i++, c = T[u].right) {
    if (i)
      cout << ", ";
    cout << c;
  }
  cout << "]" << endl;
}

// ????????°?±???±???
int rec(int u, int p) {
  depth[u] = p;
  //?????§???????????????????????±???
  if (T[u].right != NIL)
    rec(T[u].right, p);
  //????????§??????????????±?????????????????±?????±???+1
  if (T[u].leaf != NIL)
    rec(T[u].leaf, p + 1);
}

int main() {
  int d;
  int v;
  int c;
  int l;
  int r;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> v >> d;
    for (int j = 0; j < d; j++) {
      cin >> c;
      if (j == 0)
        T[v].leaf = c;
      else
        T[l].right = c;
      l = c;
      T[c].parent = v;
    }
  }
  for (int i = 0; i < n; i++) {
    if (T[i].parent == NIL)
      r = i;
  }
  rec(r, 0);
  for (int i = 0; i < n; i++)
    print(i);
}
Problem p02279 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#define MAX 100005
#define NIL -1
struct Node {
  int p; // parent
  int l; // left-child
  int r; // right-sibling
};

struct Node T[MAX];
int n, D[MAX]; // D[i]: save the depth from root to point i

void setDepth(int u, int p) {
  D[u] = p;
  if (T[u].r != NIL)
    setDepth(T[u].r, p);
  if (T[u].l != NIL)
    setDepth(T[u].l, p + 1);
}

int getDepth(int u) {
  int d = 0;
  while (T[u].p != NIL) {
    u = T[u].p;
    d++;
  }
  return d;
}

void printChildren(int u) {
  printf("[");
  for (int i = 0, c = T[u].l; c != NIL; c = T[c].r, i++) {
    if (i)
      printf(", ");
    printf("%d", c);
  }

  printf("]\\n");
}

void makeTree() {
  int i, j, d, v, c, l, r, u;
  for (i = 0; i < n; i++)
    T[i].p = T[i].r = T[i].l = NIL;

  for (i = 0; i < n; i++) {
    scanf("%d %d", &v, &d); // v: parent d: the number of childs
    for (j = 0; j < d; j++) {
      scanf("%d", &c); // c: child
      if (j == 0)
        T[v].l = c; // most left child for v(parent)
      else
        T[l].r = c;
      l = c;      // set the child number to l
      T[c].p = v; // set the parent number for c(children)
    }
  }
}
int main(void) {
  int i, j, d, v, c, l, r, u;
  scanf("%d", &n);

  makeTree();

  for (i = 0; i < n; i++) {
    if (T[i].p == NIL)
      r = i; // point i is root because i don't have a parent.
  }
  setDepth(r, 0);

  for (u = 0; u < n; u++) {

    printf("node %d: parent = %d, depth = %d, ", u, T[u].p, getDepth(u));

    if (T[u].p == NIL)
      printf("root, ");
    else if (T[u].l == NIL)
      printf("leaf, ");
    else
      printf("internal node, ");

    printChildren(u);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
using namespace std;
#define MAX 10005
#define NIL -1
struct Node {
  int p, l, r;
};

Node T[MAX];
int n, D[MAX];

void rec(int u, int p) {
  D[u] = p;
  if (T[u].r != NIL)
    rec(T[u].r, p);
  if (T[u].l != NIL)
    rec(T[u].l, p + 1);
}

int main(void) {
  int i, j, d, v, c, l, r, u;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    T[i].p = T[i].r = T[i].l = NIL;

  for (i = 0; i < n; i++) {
    scanf("%d %d", &v, &d);
    for (j = 0; j < d; j++) {
      scanf("%d", &c);
      if (j == 0)
        T[v].l = c;
      else
        T[l].r = c;
      l = c;
      T[c].p = v;
    }
  }

  for (i = 0; i < n; i++) {
    if (T[i].p == NIL)
      r = i;
  }
  rec(r, 0);

  for (u = 0; u < n; u++) {

    printf("node %d: parent = %d, depth = %d, ", u, T[u].p, D[u]);

    if (T[u].p == NIL)
      printf("root, ");
    else if (T[u].l == NIL)
      printf("leaf, ");
    else
      printf("internal node, ");

    printf("[");
    for (i = 0, c = T[u].l; c != NIL; c = T[c].r, i++) {
      if (i)
        printf(", ");
      printf("%d", c);
    }

    printf("]\\n");
  }
  return 0;
}
Problem p02279 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define maxx 100010
#define NIL -1

struct node {
  int num;
  int parent, left, right; // left is son, right is brother
};
node tree[maxx];

int get_depth(int x) {
  int depth = 0;
  while (tree[x].parent != NIL) {
    // cout<<"  "<<x<<"  ";
    x = tree[x].parent;
    depth++;
  }
  return depth;
}

int main() {
  for (int i = 0; i < maxx; i++) {
    tree[i].parent = NIL;
    tree[i].left = NIL;
    tree[i].right = NIL;
  }
  int n;
  cin >> n;
  int a, b, c, d;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    tree[i].num = a;
    for (int j = 0; j < b; j++) {
      if (j == 0) {
        cin >> c;
        tree[tree[i].num].left = c;
        tree[c].parent = tree[i].num;
      } else {
        cin >> d;
        tree[c].right = d;
        tree[d].parent = tree[i].num;
        c = d;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    printf("node %d: parent = %d, depth = %d, ", i, tree[i].parent,
           get_depth(i));
    if (tree[i].parent == NIL)
      printf("root, [");
    else if (tree[i].left == NIL)
      printf("leaf, [");
    else
      printf("internal node, [");
    if (tree[i].left != NIL) {
      printf("%d", tree[i].left);
      int xx = tree[i].left;
      while (tree[xx].right != NIL) {
        printf(", %d", tree[xx].right);
        xx = tree[xx].right;
      }
    }
    printf("]\\n");
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define maxx 100010
#define NIL -1

struct node {
  int num;
  int parent, left, right; // left is son, right is brother
};
node tree[maxx];

int get_depth(int x) {
  int depth = 0;
  while (tree[x].parent != NIL) {
    x = tree[x].parent;
    depth++;
  }
  return depth;
}

int main() {
  for (int i = 0; i < maxx; i++) {
    tree[i].parent = NIL;
    tree[i].left = NIL;
    tree[i].right = NIL;
  }
  int n;
  cin >> n;
  int a, b, c, d;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    tree[i].num = a;
    for (int j = 0; j < b; j++) {
      if (j == 0) {
        cin >> c;
        tree[i].left = c;
        tree[c].parent = i;
      } else {
        cin >> d;
        tree[c].right = d;
        tree[d].parent = i;
        c = d;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    printf("node %d: parent = %d, depth = %d, ", tree[i].num, tree[i].parent,
           get_depth(i));
    if (tree[i].parent == NIL)
      printf("root, [");
    else if (tree[i].left == NIL)
      printf("leaf, [");
    else
      printf("internal node, [");
    if (tree[i].left != NIL) {
      printf("%d", tree[i].left);
      int xx = tree[i].left;
      while (tree[xx].right != NIL) {
        printf(", %d", tree[xx].right);
        xx = tree[xx].right;
      }
    }
    printf("]\\n");
  }
  return 0;
}
Problem p02279 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdlib>
#include <iostream>
using namespace std;

#define MAX 100005
#define NIL -1

struct Node {
  int parent;
  int left;
  int right;
};

Node T[MAX];
int n, D[MAX];

void print(int u) {
  cout << "node " << u << ": ";
  cout << "parent = " << T[u].parent << ", ";
  cout << "depth = " << D[u] << ", ";

  if (T[u].parent == NIL)
    cout << "root, ";
  else if (T[u].left == NIL)
    cout << "leaf, ";
  else
    cout << "internal node, ";

  cout << "[";
  for (int i = 0, c = T[u].left; c != NIL; i++, c = T[c].right) {
    if (i)
      cout << ", ";
    cout << c;
  }
  cout << "]" << endl;
}

int rec(int u, int p) {
  D[u] = p;
  if (T[u].right != NIL)
    rec(T[u].right, p);
  if (T[u].left != NIL)
    rec(T[u].left, p + 1);
}

int main() {
  int d, v, c, l, r;
  cin >> n;
  for (int i = 0; i < n; i++)
    T[i].parent = T[i].left = T[i].right = NIL;

  for (int i = 0; i < n; i++) {
    cin >> v >> d;
    for (int j = 0; j < d; j++) {
      cin >> c;
      if (j == 0)
        T[v].left = c;
      else
        T[l].right = c;
      l = c;
      T[c].parent = v;
    }
  }
  for (int i = 0; i < n; i++) {
    if (T[i].parent == NIL)
      r = i;
  }

  rec(r, 0);

  for (int i = 0; i < n; i++)
    print(i);

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdlib>
#include <iostream>
using namespace std;

#define MAX 100005
#define NIL -1

struct Node {
  int parent;
  int left;
  int right;
};

Node T[MAX];
int n, D[MAX];

void print(int u) {
  cout << "node " << u << ": ";
  cout << "parent = " << T[u].parent << ", ";
  cout << "depth = " << D[u] << ", ";

  if (T[u].parent == NIL)
    cout << "root, ";
  else if (T[u].left == NIL)
    cout << "leaf, ";
  else
    cout << "internal node, ";

  cout << "[";
  for (int i = 0, c = T[u].left; c != NIL; i++, c = T[c].right) {
    if (i)
      cout << ", ";
    cout << c;
  }
  cout << "]" << endl;
}

int rec(int u, int p) {
  D[u] = p;
  if (T[u].right != NIL)
    rec(T[u].right, p);
  if (T[u].left != NIL)
    rec(T[u].left, p + 1);
}

int main() {
  int d, v, c, l, r;
  cin >> n;
  for (int i = 0; i < n; i++)
    T[i].parent = T[i].left = T[i].right = NIL;

  for (int i = 0; i < n; i++) {
    cin >> v >> d;
    for (int j = 0; j < d; j++) {
      cin >> c;
      if (j == 0)
        T[v].left = c;
      else
        T[l].right = c;
      l = c;
      T[c].parent = v;
    }
    for (int i = 0; i < n; i++) {
      if (T[i].parent == NIL)
        r = i;
    }
  }

  rec(r, 0);

  for (int i = 0; i < n; i++)
    print(i);

  return 0;
}
Problem p02279 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define llong long long
#define IFY 2000000000
#define MAX 100005
#define NIL -1
using namespace std;
struct Node {
  int p, l, r;
};
Node T[MAX];
int D[MAX];
void print(int u) {
  int j, c;
  printf("node %d: ", u);
  printf("parent = %d, ", T[u].p);
  printf("depth = %d, ", D[u]);
  if (T[u].p == NIL)
    printf("root, ");
  else if (T[u].l == NIL)
    printf("leaf, ");
  else
    printf("internal node, ");
  printf("[");
  for (j = 0, c = T[u].l; c != NIL; j++, c = T[c].r) {
    if (j)
      printf(", ");
    printf("%d", c);
  }
  printf("]\\n");
}
int rec(int u, int p) {
  D[u] = p;
  if (T[u].l != NIL)
    rec(T[u].l, p + 1);
  if (T[u].r != NIL)
    rec(T[u].r, p);
}
int main() {
  int n, v, d, c, l, r, j;
  scanf("%d", &n);
  REP(i, n) T[i].p = T[i].l = T[i].r = NIL;
  REP(i, n) {
    scanf("%d %d", &v, &d);
    REP(j, d) {
      scanf("%d", &c);
      if (j == 0)
        T[v].l = c;
      else
        T[l].r = c;
      l = c;
      T[c].p = v;
    }
  }
  REP(i, n) if (T[i].p == NIL) r = i;
  rec(r, 0);
  REP(i, n) print(i);
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define llong long long
#define IFY 2000000000
#define MAX 100005
#define NIL -1
using namespace std;
struct Node {
  int p, l, r;
};
Node T[MAX];
int D[MAX];
void print(int u) {
  int j, c;
  printf("node %d: ", u);
  printf("parent = %d, ", T[u].p);
  printf("depth = %d, ", D[u]);
  if (T[u].p == NIL)
    printf("root, ");
  else if (T[u].l == NIL)
    printf("leaf, ");
  else
    printf("internal node, ");
  printf("[");
  for (j = 0, c = T[u].l; c != NIL; j++, c = T[c].r) {
    if (j)
      printf(", ");
    printf("%d", c);
  }
  printf("]\\n");
}
int rec(int u, int p) {
  D[u] = p;
  if (T[u].l != NIL)
    rec(T[u].l, p + 1);
  if (T[u].r != NIL)
    rec(T[u].r, p);
}
int main() {
  int n, v, d, c, l, r, j;
  scanf("%d", &n);
  REP(i, n) T[i].p = T[i].l = T[i].r = NIL;
  REP(i, n) {
    scanf("%d %d", &v, &d);
    REP(j, d) {
      scanf("%d", c);
      if (j == 0)
        T[v].l = c;
      else
        T[l].r = c;
      l = c;
      T[c].p = v;
    }
  }
  REP(i, n) {
    if (T[i].p == NIL)
      r = i;
  }
  rec(r, 0);
  REP(i, n) print(i);
  return 0;
}
Problem p02279 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

vector<int> E[100000];
int d[100000], pr[100000];

void rec(int v, int p) {
  d[v] = p;
  for (int u : E[v])
    rec(u, p + 1);
}
int main() {
  int n;
  scanf("%d", &n);
  memset(pr, -1, sizeof(pr));
  rep(i, n) {
    int id, k;
    scanf("%d%d", &id, &k);
    rep(j, k) {
      int c;
      scanf("%d", &c);
      E[id].push_back(c);
      pr[c] = id;
    }
  }
  rep(i, n) {
    if (!~pr[i]) {
      rec(i, 0);
      break;
    }
  }
  rep(i, n) {
    printf("node %d: parent = %d, depth = %d, ", i, pr[i], d[i]);
    if (!~pr[i])
      printf("root, [");
    else if (E[i].empty())
      printf("leaf, [");
    else
      printf("internal node, [");
    rep(j, E[i].size()) {
      if (j)
        printf(", ");
      printf("%d", E[i][j]);
    }
    printf("]\\n");
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

vector<int> E[10000];
int d[100000], pr[100000];

void rec(int v, int p) {
  d[v] = p;
  for (int u : E[v])
    rec(u, p + 1);
}
int main() {
  int n;
  scanf("%d", &n);
  memset(pr, -1, sizeof(pr));
  rep(i, n) {
    int id, k;
    scanf("%d%d", &id, &k);
    rep(j, k) {
      int c;
      scanf("%d", &c);
      E[id].push_back(c);
      pr[c] = id;
    }
  }
  rep(i, n) {
    if (!~pr[i]) {
      rec(i, 0);
      break;
    }
  }
  rep(i, n) {
    printf("node %d: parent = %d, depth = %d, ", i, pr[i], d[i]);
    if (!~pr[i])
      printf("root, [");
    else if (E[i].empty())
      printf("leaf, [");
    else
      printf("internal node, [");
    rep(j, E[i].size()) {
      if (j)
        printf(", ");
      printf("%d", E[i][j]);
    }
    printf("]\\n");
  }
}
Problem p02280 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

struct Node {
  int sib, p, k, left, right;
  Node() { sib = p = k = left = right = -1; }
};

int calcHeight(int id, Node *node, int height) {
  if (id == -1)
    return -1;
  if (node[id].k == 0)
    return height;
  if (height > 25)
    return -1;
  else
    return max(calcHeight(node[id].left, node, height + 1),
               calcHeight(node[id].right, node, height + 1));
}

int main(void) {
  int n, i, k, id, left, right, depth;
  Node node[25];

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> id >> left >> right;
    k = 0;
    node[id].left = left;
    node[id].right = right;
    if (left != -1) {
      node[left].sib = right;
      node[left].p = id;
      k++;
    }
    if (right != -1) {
      node[right].sib = left;
      node[right].p = id;
      k++;
    }
    node[id].k = k;
  }

  for (i = 0; i < n; i++) {
    id = i;
    depth = 0;
    for (int j = 0; j < n; j++) {
      if (node[id].p == -1)
        break;
      id = node[id].p;
      depth++;
    }
    /*
    while(node[id].p!=-1){
        id=node[id].p;
        depth++;
    }
    */
    printf("node %d: parent = %d, sibling = %d, degree = %d, depth = %d, "
           "height = %d, ",
           i, node[i].p, node[i].sib, node[i].k, depth, calcHeight(i, node, 0));
    if (depth == 0)
      printf("root\\n");
    else if (node[i].k > 0)
      printf("internal node\\n");
    else
      printf("leaf\\n");
  }
  return 0;
}


Error: -11

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

struct Node {
  int sib, p, k, left, right;
};

int calcHeight(int id, Node *node, int height) {
  if (id == -1)
    return -1;
  if (node[id].k == 0)
    return height;
  else
    return max(calcHeight(node[id].left, node, height + 1),
               calcHeight(node[id].right, node, height + 1));
}

int main(void) {
  int n, i, k, id, left, right, depth;
  Node node[25];

  cin >> n;
  for (i = 0; i < n; i++)
    node[i].p = node[i].sib = -1;
  for (i = 0; i < n; i++) {
    cin >> id >> left >> right;
    k = 0;
    node[left].p = node[right].p = id;
    node[id].left = left;
    node[id].right = right;
    if (left != -1) {
      node[left].sib = right;
      k++;
    }
    if (right != -1) {
      node[right].sib = left;
      k++;
    }
    node[id].k = k;
  }

  for (i = 0; i < n; i++) {
    id = i;
    depth = 0;
    while (node[id].p != -1) {
      id = node[id].p;
      depth++;
    }
    printf("node %d: parent = %d, siblig = %d, degree = %d, depth = %d, height "
           "= %d, ",
           i, node[i].p, node[i].sib, node[i].k, depth, calcHeight(i, node, 0));
    if (depth == 0)
      printf("root\\n");
    else if (node[i].k > 0)
      printf("internal node\\n");
    else
      printf("leaf\\n");
  }
  return 0;
}
Problem p02280 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define N 25
using namespace std;
int n, a, l[N], r[N], par[N], sib[N], deg[N], dep[N], height[N], z[N];
bool used[N];

int dfs(int x, int d) {
  dep[x] = d;
  if (l[x] == -1 && r[x] == -1)
    return 1;
  if (z[x] != 2)
    z[x] = 1;
  if (l[x] != -1)
    height[x] = max(height[x], dfs(l[x], d + 1));
  if (r[x] != -1)
    height[x] = max(height[x], dfs(r[x], d + 1));
  return height[x] + 1;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    cin >> l[a] >> r[a];
    if (l[a] != -1)
      deg[a]++, used[l[a]] = true, par[l[a]] = a, sib[l[a]] = r[a];
    if (r[a] != -1)
      deg[a]++, used[r[a]] = true, par[r[a]] = a, sib[r[a]] = l[a];
  }
  for (int i = 0; i < n; i++)
    if (!used[i])
      par[i] = -1, sib[i] = -1, z[i] = 2, dfs(i, 0);
  for (int i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << par[i] << ", sibling = " << sib[i];
    cout << ", degree = " << deg[i] << ", depth = " << dep[i]
         << ", height = " << height[i];
    if (!z[i])
      cout << ", leaf" << endl;
    if (z[i] == 1)
      cout << ", internal node" << endl;
    if (z[i] == 2)
      cout << ", root" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define N 25
using namespace std;
int n, a, l[N], r[N], par[N], sib[N], deg[N], dep[N], height[N], z[N];
bool used[N];

int dfs(int x, int d) {
  dep[x] = d;
  if (l[x] == -1 && r[x] == -1)
    return 1;
  if (z[x] != 2)
    z[x] = 1;
  height[x] = max(height[x], dfs(l[x], d + 1));
  height[x] = max(height[x], dfs(r[x], d + 1));
  return height[x] + 1;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    cin >> l[a] >> r[a];
    if (l[a] == -1)
      continue;
    deg[a] = 2;
    if (l[a] != -1)
      used[l[a]] = true, par[l[a]] = a, sib[l[a]] = r[a];
    if (r[a] != -1)
      used[r[a]] = true, par[r[a]] = a, sib[r[a]] = l[a];
  }
  for (int i = 0; i < n; i++)
    if (!used[i])
      par[i] = -1, sib[i] = -1, z[i] = 2, dfs(i, 0);
  for (int i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << par[i] << ", sibling = " << sib[i];
    cout << ", degree = " << deg[i] << ", depth = " << dep[i]
         << ", height = " << height[i];
    if (!z[i])
      cout << ", leaf" << endl;
    if (z[i] == 1)
      cout << ", internal node" << endl;
    if (z[i] == 2)
      cout << ", root" << endl;
  }
  return 0;
}
Problem p02280 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#define MAX 10000
#define NIL -1

struct Node {
  int parent, left, right;
};

Node T[MAX];
int n, D[MAX], H[MAX];

void setDepth(int u, int d) {
  if (u == NIL)
    return;
  D[u] = d;
  setDepth(T[u].left, d + 1);
  setDepth(T[u].right, d + 1);
}

int setHeight(int u) {
  int h1 = 0, h2 = 0;
  if (T[u].left != NIL)
    h1 = setHeight(T[u].left) + 1;
  if (T[u].right != NIL)
    h2 = setHeight(T[u].right) + 1;
  return H[u] = (h1 > h2 ? h1 : h2);
}

int getSibling(int u) {
  if (T[u].parent == NIL)
    return NIL;
  if (T[T[u].parent].left != u && T[T[u].parent].left != NIL)
    return T[T[u].parent].left;
  if (T[T[u].parent].right != u && T[T[u].parent].right != NIL)
    return T[T[u].parent].right;
  return NIL;
}

void print(int u) {
  printf("node %d: ", u);
  printf("parent = %d, ", T[u].parent);
  printf("sibling = %d, ", getSibling(u));
  int deg = 0;
  if (T[u].left != NIL)
    deg++;
  if (T[u].right != NIL)
    deg++;
  printf("degree = %d, ", deg);
  printf("depth = %d, ", D[u]);
  printf("height = %d, ", H[u]);

  if (T[u].parent == NIL) {
    printf("root\\n");
  } else if (T[u].left == NIL && T[u].right == NIL) {
    printf("leaf\\n");
  } else {
    printf("internal node\\n");
  }
}

int main() {
  int v, l, r, root = 0;
  scanf("%d", &n);

  for (int i = 0; i < n; i++)
    T[i].parent = NIL;

  for (int i = 0; i < n; i++) {
    scanf("%d %d %d", &v, &l, &r);
    T[v].left = l;
    T[v].right = r;
    if (l != NIL)
      T[l].parent = v;
    if (r != NIL)
      T[r].parent = v;
  }
  for (int i = 0; i < n; i++)
    if (T[i].parent == NIL)
      root = i;

  setDepth(root, 0);
  setHeight(root);

  for (int i = 0; i < n; i++)
    print(i);
  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
using namespace std;
#define MAX 100005
#define NIL -1

struct Node {
  int p, l, r;
};

Node T[MAX];
int n, D[MAX];

void print(int u) {
  int i, c;
  cout << "node" << u << ": ";
  cout << "parent = " << T[u].p << ", ";
  cout << "depth = " << D[u] << ", ";

  if (T[u].p == NIL)
    cout << "root, ";
  else if (T[u].l == NIL)
    cout << "leaf, ";
  else
    cout << "internal node, ";

  cout << "[";

  for (i = 0, c = T[u].l; c != NIL; i++, c = T[c].r) {
    if (i)
      cout << ", ";
    cout << c;
  }
  cout << "]" << endl;
}

void rec(int u, int p) {
  D[u] = p;
  if (T[u].r != NIL)
    rec(T[u].r, p);
  if (T[u].l != NIL)
    rec(T[u].l, p + 1);
}

int main() {
  int i, j, d, v, c, l, r;
  cin >> n;
  for (i = 0; i < n; i++)
    T[i].p = T[i].l = T[i].r = NIL;

  for (i = 0; i < n; i++) {
    cin >> v >> d;
    for (j = 0; j < d; j++) {
      cin >> c;
      if (j == 0)
        T[v].l = c;
      else
        T[l].r = c;
      l = c;
      T[c].p = v;
    }
  }
  for (i = 0; i < n; i++) {
    if (T[i].p == NIL)
      r = i;
  }

  rec(r, 0);
  for (i = 0; i < n; i++)
    print(i);
  return 0;
}