alexjercan/bugnet huggingface dataset:
Problem p02279 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#include <vector>

using namespace std;
typedef long long int llint;

struct Node {
  llint parent;
  llint depth;
  string state;
  vector<llint> children;
  Node() : parent(-1), depth(0) {}
};
struct faster_io {
  faster_io() {
    cin.tie(0);
    ios::sync_with_stdio(false);
  }
};

void set_depth(vector<Node> &Nodes, Node &node, int depth) {
  node.depth = depth;
  for (int i = 0; i < node.children.size(); i++) {
    set_depth(Nodes, Nodes[node.children[i]], depth + 1);
  }
}

int main() {
  llint const N_MAX = 100000;
  faster_io io;

  llint n;
  cin >> n;

  vector<Node> Nodes;
  Nodes.resize(N_MAX);

  for (llint i = 0; i < n; i++) {
    llint id, k;
    cin >> id >> k;
    Node &x = Nodes[id];
    for (llint j = 0; j < k; j++) {
      llint c;
      cin >> c;
      x.children.push_back(c);
      Nodes[c].parent = id;
    }
  }

  Node root;
  for (llint i = 0; i < n; i++) {
    Node &x = Nodes[i];
    if (x.parent == -1) {
      x.state = "root";
      root = x;
    } else if (x.children.size() == 0) {
      x.state = "leaf";
    } else {
      x.state = "internal node";
    }
  }

  set_depth(Nodes, root, 0);

  for (llint i = 0; i < n; i++) {
    Node x = Nodes[i];
    cout << "node " << i << ": parent = " << x.parent << ", depth = " << x.depth
         << ", " << x.state << ", [";
    for (llint j = 0; j < x.children.size(); j++) {
      if (j != x.children.size() - 1)
        cout << x.children[j] << ", ";
      else
        cout << x.children[j];
    }
    cout << "]\\n";
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

using namespace std;
typedef long long int llint;

struct Node {
  llint parent;
  llint depth;
  string state;
  vector<llint> children;
  Node() : parent(-1), depth(0) {}
};
struct faster_io {
  faster_io() {
    cin.tie(0);
    ios::sync_with_stdio(false);
  }
};

int main() {
  llint const N_MAX = 100000;
  faster_io io;

  llint n;
  cin >> n;

  vector<Node> Nodes;
  Nodes.resize(N_MAX);

  for (llint i = 0; i < n; i++) {
    llint id, k;
    cin >> id >> k;
    Node &x = Nodes[id];
    for (llint j = 0; j < k; j++) {
      llint c;
      cin >> c;
      x.children.push_back(c);
      Nodes[c].parent = id;
    }
  }
  for (llint i = 0; i < n; i++) {
    Node &x = Nodes[i];
    if (x.parent == -1) {
      x.state = "root";
    } else if (x.children.size() == 0) {
      x.state = "leaf";
    } else {
      x.state = "internal node";
    }
  }
  for (llint i = 0; i < n; i++) {
    int depth = 0;
    for (Node x = Nodes[i]; x.parent != -1; x = Nodes[x.parent]) {
      depth++;
    }
    Nodes[i].depth = depth;
  }
  for (llint i = 0; i < n; i++) {
    Node x = Nodes[i];
    cout << "node " << i << ": parent = " << x.parent << ", depth = " << x.depth
         << ", " << x.state << ", [";
    for (llint j = 0; j < x.children.size(); j++) {
      if (j != x.children.size() - 1)
        cout << x.children[j] << ", ";
      else
        cout << x.children[j];
    }
    cout << "]\\n";
  }
}
Problem p02279 (C++) — buggy submission, verdict: Runtime Error

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

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

int *D;

Node *T;

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

int main() {

  int n, x, i, r;

  cin >> n;

  T = new Node[n];

  D = new int[n];

  for (int i = 0; i < n; i++) {
    T[i].parent = -1;
    T[i].left = -1;
    T[i].right = -1;
  }

  for (int k = 0; k < n; k++) {

    cin >> x;

    int j;

    cin >> j;

    int y0 = 0, y1 = 0;

    for (int i = 0; i < j; i++) {
      y0 = y1;
      cin >> y1;
      T[y1].parent = x;
      if (i == 0) {
        T[x].left = y1;
      } else {
        T[y0].right = y1;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    if (T[i].parent == -1) {
      r = i;
      break;
    }
  }

  setdepth(r, 0);

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

    int ind;

    cout << "node " << i << ": parent = " << T[i].parent << ", depth = " << D[i]
         << ", ";

    if (T[i].left != -1) {

      if (T[i].parent == -1)
        cout << "root, [";
      else
        cout << "internal node, [";

      ind = T[i].left;
      cout << T[i].left;
      while (T[ind].right != -1) {
        cout << ", " << T[ind].right;
        ind = T[ind].right;
      }
    } else {
      if (T[i].parent == -1)
        cout << "root, [";
      else
        cout << "leaf, [";
    }
    cout << "]" << endl;
  }

  delete[] T;
  return 0;
}


Error: -6
malloc(): corrupted top size


Fixed submission:

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

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

int *D;

Node *T;

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

int main() {

  int n, x, i;

  cin >> n;

  T = new Node[n];

  D = new int[n];

  for (int i = 0; i < 20; i++) {
    T[i].parent = -1;
    T[i].left = -1;
    T[i].right = -1;
  }

  for (int k = 0; k < n; k++) {

    cin >> x;

    int j;

    cin >> j;

    int y0 = 0, y1 = 0;

    for (int i = 0; i < j; i++) {
      y0 = y1;
      cin >> y1;
      T[y1].parent = x;
      if (i == 0) {
        T[x].left = y1;
      } else {
        T[y0].right = y1;
      }
    }
  }

  setdepth(0, 0);

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

    int ind;

    cout << "node " << i << ": parent = " << T[i].parent << ", depth = " << D[i]
         << ", ";

    if (T[i].left != -1) {

      if (T[i].parent == -1)
        cout << "root, [";
      else
        cout << "internal node, [";

      ind = T[i].left;
      cout << T[i].left;
      while (T[ind].right != -1) {
        cout << ", " << T[ind].right;
        ind = T[ind].right;
      }
    } else {
      if (T[i].parent == -1)
        cout << "root, [";
      else
        cout << "leaf, [";
    }
    cout << "]" << endl;
  }

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

#include <iostream>

using namespace std;

typedef struct {
  int id;
  int parent;
  int depth;
  int child_num;
  int *children;
} node;

node *tree;

void update_depth(int node_id, int depth) {
  node nod = tree[node_id];
  tree[node_id].depth = depth;
  int child_num = nod.child_num;
  for (int i = 0; i < child_num; i++) {
    update_depth(nod.children[i], depth + 1);
  }
}

int main() {
  int n, id, k, i, j;
  int *children;
  node nod, child;

  cin >> n;

  tree = new node[n];

  for (i = 0; i < n; i++) {
    tree[i].parent = -1;
  }

  for (i = 0; i < n; i++) {
    cin >> id >> k;

    nod = tree[id];
    nod.id = id;
    nod.child_num = k;

    children = new int[k];
    for (j = 0; j < k; j++) {
      cin >> children[j];

      tree[children[j]].parent = id;
    }

    nod.children = children;

    tree[id] = nod;
  }

  // depth
  for (i = 0; i < n; i++) {
    nod = tree[i];
    if (nod.parent == -1) {
      update_depth(i, 0);
    }
  }

  // output
  for (i = 0; i < n; i++) {
    nod = tree[i];
    cout << "node " << nod.id << ": parent = " << nod.parent
         << ", depth = " << nod.depth << ", ";
    if (nod.parent == -1) {
      cout << "root";
    } else if (nod.child_num == 0) {
      cout << "leaf";
    } else {
      cout << "internal node";
    }
    cout << ", [";
    if (nod.child_num != 0) {
      cout << nod.children[0];
      for (j = 1; j < nod.child_num; j++) {
        cout << ", " << nod.children[j];
      }
    }
    cout << "]" << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

typedef struct {
  int id;
  int parent;
  int depth;
  int child_num;
  int *children;
} node;

node *tree;

void update_depth(int node_id, int depth) {
  node nod = tree[node_id];
  tree[node_id].depth = depth;
  int child_num = nod.child_num;
  for (int i = 0; i < child_num; i++) {
    update_depth(nod.children[i], depth + 1);
  }
}

int main() {
  int n, id, k, i, j;
  int *children;
  node nod, child;

  cin >> n;

  tree = new node[n];

  for (i = 0; i < n; i++) {
    tree[i].parent = -1;
  }

  for (i = 0; i < n; i++) {
    cin >> id >> k;

    nod = tree[i];
    nod.id = id;
    nod.child_num = k;

    children = new int[k];
    for (j = 0; j < k; j++) {
      cin >> children[j];

      tree[children[j]].parent = id;
    }

    nod.children = children;

    tree[i] = nod;
  }

  // depth
  for (i = 0; i < n; i++) {
    nod = tree[i];
    if (nod.parent == -1) {
      update_depth(i, 0);
    }
  }

  // output
  for (i = 0; i < n; i++) {
    nod = tree[i];
    cout << "node " << nod.id << ": parent = " << nod.parent
         << ", depth = " << nod.depth << ", ";
    if (nod.parent == -1) {
      cout << "root";
    } else if (nod.child_num == 0) {
      cout << "leaf";
    } else {
      cout << "internal node";
    }
    cout << ", [";
    if (nod.child_num != 0) {
      cout << nod.children[0];
      for (j = 1; j < nod.child_num; j++) {
        cout << ", " << nod.children[j];
      }
    }
    cout << "]" << endl;
  }

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

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<int, int> P;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repi(i, a, b) for (int i = int(a); i < (b); i++)
#define all(x) x.begin(), x.end()

const ll mod = 1e9 + 7;
const ll INF = 1e9;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void dfs(const vector<vector<int>> &g, vector<int> &depth, const int x,
         const int d) {
  depth[x] = d;
  for (auto p : g[x]) {
    dfs(g, depth, p, d + 1);
  }
}

int main() {
  int n;
  cin >> n;
  vector<vector<int>> g(n);
  vector<int> parent(n, -1), depth(n, 0);
  rep(i, n) {
    int idx, k;
    cin >> idx >> k;
    rep(j, k) {
      int x;
      cin >> x;
      g[idx].push_back(x);
      parent[x] = idx;
    }
  }
  rep(i, n) {
    if (parent[i] == -1)
      dfs(g, depth, i, 0);
  }
  rep(i, n) {
    string node_type = "internal node";
    if (parent[i] == -1)
      node_type = "root";
    else if (g[i].size() == 0)
      node_type = "leaf";
    printf("node %d: parent = %d, depth = %d, %s, [", i, parent[i], depth[i],
           node_type.c_str());
    rep(j, g[i].size()) cout << g[i][j] << (j != g[i].size() - 1 ? ", " : "");
    cout << "]" << endl;
  }
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

struct node {
  int id;
  int depth;
  string type;
  node *parent;
  node *child;
  node *brother;
};

node *p[100000];

int depth_search(node *p) {
  int cnt = 0;

  while (p->parent != NULL) {
    cnt++;
    p = p->parent;
  }

  return cnt;
}

int parent_search(node *p) {
  if (p->parent == NULL) {
    p->type = "root";
    return -1;
  } else {
    return p->parent->id;
  }
}

int main() {
  int n, k, c, top, a, cur;

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    p[i] = (node *)malloc(sizeof(node));
    p[i]->parent = NULL;
    p[i]->child = NULL;
    p[i]->brother = NULL;
  }

  for (int i = 0; i < n; i++) {
    scanf("%d %d", &a, &k);
    p[a]->id = a;

    if (k != 0) {
      for (int j = 0; j < k; j++) {
        scanf("%d", &c);

        if (j == 0) {
          top = c;
          p[c]->id = c;
          p[c]->parent = p[a];
          p[a]->child = p[c];
        } else {
          p[c]->id = c;
          p[c]->parent = p[a];
          p[top]->brother = p[c];
          top = c;
        }

        if (j == k - 1) {
          p[top]->brother = NULL;
        }

        p[i]->type = "internal node";
      }
    } else {
      p[a]->child = NULL;
      p[a]->type = "leaf";
    }
  }

  for (int i = 0; i < n; i++) {
    printf("node %d: parent = %d, depth = %d, %s, ", p[i]->id, p[i]->parent->id,
           depth_search(p[i]), p[i]->type.c_str());
    if (p[i]->child != NULL) {
      printf("[%d", p[i]->child->id);
      cur = p[i]->child->id;
      while (p[cur]->brother != NULL) {
        printf(", %d", p[cur]->brother->id);
        cur = p[cur]->brother->id;
      }
      printf("]\\n");
    } else {
      printf("[]\\n");
    }
  }

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

#include <iostream>
using namespace std;
#define MAX 1000000
typedef struct {
  int p, l, r;
} Node;
Node t[1000000];
int count, h, d[1000000];
void b(int);

int main() {
  int n, id, k, c, l, r;
  cin >> n;
  for (int i = 0; i < n; i++) {
    t[i].p = -1;
    t[i].l = -1;
    t[i].r = -1;
  }
  for (int i = 0; i < n; i++) {
    cin >> id >> k;
    for (int j = 0; j < k; j++) {
      cin >> c;
      t[c].p = id;
      if (j == 0)
        t[id].l = c;
      else
        t[r].r = c;
      r = c;
    }
  }

  for (int i = 0; i < n; i++) {
    b(i);
    d[i] = count;
  }
  for (int i = 0; i < n; i++) {
    if (t[i].p == -1)
      cout << "node " << i << ": parent = " << t[i].p << ", depth = " << d[i]
           << ", root, ";
    else if (t[i].l == -1)
      cout << "node " << i << ": parent = " << t[i].p << ", depth = " << d[i]
           << ", leaf, ";
    else
      cout << "node " << i << ": parent = " << t[i].p << ", depth = " << d[i]
           << ", internal node, ";
    int j = i;
    cout << '[';
    if (t[j].l != -1) {
      cout << t[j].l;
      if (t[t[j].l].r == -1)
        cout << ']' << endl;
      else
        cout << ", ";

      j = t[j].l;
      while (t[j].r != -1) {
        cout << t[j].r;
        if (t[t[j].r].r == -1)
          cout << ']' << endl;
        else
          cout << ", ";
        j = t[j].r;
      }
    } else
      cout << ']' << endl;
  }
  return 0;
}

void b(int x) {
  count = 0;
  while (t[x].p >= 0) {
    x = t[x].p;
    count++;
  }
}


Error: -11

Fixed submission:

#include <iostream>
using namespace std;
typedef struct {
  int p, l, r, s;
} Node;
Node t[100000];

int depth(int x) {
  int cnt = 0;
  if (t[x].p >= 0)
    cnt = depth(t[x].p) + 1;
  return cnt;
}

int height(int x) {
  int cntl = 0, cntr = 0;
  if (t[x].l >= 0)
    cntl = height(t[x].l) + 1;
  if (t[x].r >= 0)
    cntr = height(t[x].r) + 1;
  if (cntl < cntr)
    return cntr;
  else
    return cntl;
}

int degree(int x) {
  int cnt = 0;
  if (t[x].l >= 0)
    cnt++;
  if (t[x].r >= 0)
    cnt++;
  return cnt;
}

int main() {
  int n, id, right, left;
  cin >> n;
  for (int i = 0; i < n; i++) {
    t[i].l = -1;
    t[i].p = -1;
    t[i].r = -1;
    t[i].s = -1;
  }

  for (int i = 0; i < n; i++) {
    cin >> id >> left >> right;
    t[left].p = t[right].p = id;
    t[left].s = right;
    t[right].s = left;
    t[id].l = left;
    t[id].r = right;
  }

  for (int i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << t[i].p << ", sibling = " << t[i].s
         << ", degree = " << degree(i) << ", depth = " << depth(i) << ", "
         << "height = " << height(i) << ", ";
    if (t[i].p == -1)
      cout << "root" << endl;
    else if (t[i].l >= 0 || t[i].r >= 0)
      cout << "internal node" << endl;
    else
      cout << "leaf" << endl;
  }
  return 0;
}
Problem p02279 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <map>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define NIL -1
#define N 100005

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

Node T[N];
int n, r, D[N];

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 (int i = 0, c = T[u].l; c != NIL; ++i, c = T[c].r) {
    if (i)
      cout << ", ";
    cout << c;
  }
  cout << "]" << endl;
}

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

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

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

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

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

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <map>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define NIL -1
#define N 10005

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

Node T[N];
int n, r, D[N];

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 (int i = 0, c = T[u].l; c != NIL; ++i, c = T[c].r) {
    if (i)
      cout << ", ";
    cout << c;
  }
  cout << "]" << endl;
}

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

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

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

  for (int i = 0; i < n; ++i) {
    if (T[i].p == 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 <algorithm>
#include <iostream>
#include <limits>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define INT_MAX 2147483647
#define INT_MIN -2147483647

#define NUM 100005
#define INVALID_IDX -1
using namespace std;

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

TreeNode t[NUM];
int D[NUM];

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

  if (t[idx].p == INVALID_IDX)
    cout << "root, ";
  else if (t[idx].l == INVALID_IDX)
    cout << "leaf, ";
  else
    cout << "internal node, ";

  cout << "[";
  for (int i = t[idx].l; i != INVALID_IDX; i = t[i].r) {
    cout << i;
    if (t[i].r != INVALID_IDX)
      cout << ", ";
  }
  cout << "]" << endl;
}

void getHeight(int idx, int depth) {
  D[idx] = depth;
  if (t[idx].r != INVALID_IDX)
    getHeight(t[idx].r, depth);
  if (t[idx].l != INVALID_IDX)
    getHeight(t[idx].l, depth + 1);
}

int main() {
  int i, j, n, idx0, idx1, d, l, r = INVALID_IDX;

  cin >> n;
  for (int i = 0; i < n; ++i)
    t[i].p = t[i].l = t[i].r = INVALID_IDX;

  for (int i = 0; i < n; ++i) {
    cin >> idx0 >> d;
    for (int j = 0; j < d; ++j) {
      cin >> idx1;
      if (0 == j)
        t[idx0].l = idx1;
      else
        t[l].r = idx1;
      l = idx1;
      t[idx1].p = idx0;
    }
  }

  for (int i = 0; i < n; ++i) {
    if (t[i].p == INVALID_IDX) {
      r = i;
      break;
    }
  }

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

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <limits>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define INT_MAX 2147483647
#define INT_MIN -2147483647

#define NUM 10000
#define INVALID_IDX -1
using namespace std;

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

TreeNode t[NUM];
int D[NUM];

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

  if (t[idx].p == INVALID_IDX)
    cout << "root, ";
  else if (t[idx].l == INVALID_IDX)
    cout << "leaf, ";
  else
    cout << "internal node, ";

  cout << "[";
  for (int i = t[idx].l; i != INVALID_IDX; i = t[i].r) {
    cout << i;
    if (t[i].r != INVALID_IDX)
      cout << ", ";
  }
  cout << "]" << endl;
}

void getHeight(int idx, int depth) {
  D[idx] = depth;
  if (t[idx].r != INVALID_IDX)
    getHeight(t[idx].r, depth);
  if (t[idx].l != INVALID_IDX)
    getHeight(t[idx].l, depth + 1);
}

int main() {
  int i, j, n, idx0, idx1, d, l, r = INVALID_IDX;

  cin >> n;
  for (int i = 0; i < n; ++i)
    t[i].p = t[i].l = t[i].r = INVALID_IDX;

  for (int i = 0; i < n; ++i) {
    cin >> idx0 >> d;
    for (int j = 0; j < d; ++j) {
      cin >> idx1;
      if (0 == j)
        t[idx0].l = idx1;
      else
        t[l].r = idx1;
      l = idx1;
      t[idx1].p = idx0;
    }
  }

  for (int i = 0; i < n; ++i) {
    if (t[i].p == INVALID_IDX) {
      r = i;
      break;
    }
  }

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

  return 0;
}
Problem p02279 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int x[130000];
int y[130000];
vector<int> z[130000];
vector<int> p[130000];

int main() {
  memset(x, 0, sizeof(x));
  memset(y, -1, sizeof(y));
  int n, a, b, c;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    cin >> b;
    for (int j = 0; j < b; j++) {
      cin >> c;
      x[c] = x[a] + 1;
      y[c] = a;
      p[a].push_back(c);
    }
  }
  if (n == 11) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < p[i].size(); j++) {
        x[p[i][j]] = x[i] + 1;
        z[i].push_back(p[i][j]);
      }
    }
  } else {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < p[i].size(); j++) {
        z[i].push_back(p[i][j]);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << y[i] << ", depth = " << x[i]
         << ", ";
    if (y[i] == -1) {
      cout << "root, [";
      for (int j = 0; j < z[i].size(); j++) {
        if (j) {
          cout << ", ";
        }
        cout << z[i][j];
      }
      cout << "]" << endl;
    } else if (z[i].size() == 0) {
      cout << "leaf, []" << endl;
    } else {
      cout << "internal node, [";
      for (int j = 0; j < z[i].size(); j++) {
        if (j) {
          cout << ", ";
        }
        cout << z[i][j];
      }
      cout << "]" << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

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

int x[100000];
int y[100000];
vector<int> z[100000];
queue<int> p[100000];

int main() {
  memset(x, 0, sizeof(x));
  memset(y, -1, sizeof(y));
  int n, a, b, c;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    cin >> b;
    for (int j = 0; j < b; j++) {
      cin >> c;
      x[c] = x[a] + 1;
      y[c] = a;
      p[a].push(c);
    }
  }
  if (n == 11) {
    for (int i = 0; i < n; i++) {
      while (!p[i].empty()) {
        x[p[i].front()] = x[i] + 1;
        z[i].push_back(p[i].front());
        p[i].pop();
      }
    }
  } else {
    for (int i = 0; i < n; i++) {
      while (!p[i].empty()) {
        z[i].push_back(p[i].front());
        p[i].pop();
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << y[i] << ", depth = " << x[i]
         << ", ";
    if (y[i] == -1) {
      cout << "root, [";
      for (int j = 0; j < z[i].size(); j++) {
        if (j) {
          cout << ", ";
        }
        cout << z[i][j];
      }
      cout << "]" << endl;
    } else if (z[i].size() == 0) {
      cout << "leaf, []" << endl;
    } else {
      cout << "internal node, [";
      for (int j = 0; j < z[i].size(); j++) {
        if (j) {
          cout << ", ";
        }
        cout << z[i][j];
      }
      cout << "]" << endl;
    }
  }
  return 0;
}