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

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

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

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() {
  cin >> n;

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

  int v, d, c, l, r;
  for (int i = 0; i < n; i++) {
    cin >> v >> d;
    for (int 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 (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>
using namespace std;
#define MAX 10005
#define NIL -1

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

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() {
  cin >> n;

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

  int v, d, c, l, r;
  for (int i = 0; i < n; i++) {
    cin >> v >> d;
    for (int 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 (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: Time Limit Exceeded

#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;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#define N 100000
#define MAX_NODE_DEPTH 20

int Node[N][MAX_NODE_DEPTH]; // idx:0 is node count
int Parent[N];
int n;
int root;

int getRootNode();
int getParent(int);
int getDepth(int);

int main() {
  int idx;
  int i, j;

  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &idx);          // idx
    scanf("%d", &Node[idx][0]); // node cnt
    for (j = 1; j < Node[idx][0] + 1; j++)
      scanf("%d", &Node[idx][j]);
  }

  root = getRootNode();

  for (i = 0; i < n; i++) {
    printf("node %d: ", i);
    printf("parent = %d, ", getParent(i));
    printf("depth = %d, ", getDepth(i));
    if (getParent(i) == -1) { // root
      printf("root, ");
      printf("[");
      for (j = 1; j < Node[i][0] + 1; j++) {
        printf("%d", Node[i][j]);
        if (j != Node[i][0])
          printf(", ");
      }
      printf("]\\n");
    } else if (Node[i][0] == 0) { // leaf
      // TODO
      printf("leaf, []\\n");
    } else { // internal node
      // TODO
      printf("internal node, ");
      printf("[");
      for (j = 1; j < Node[i][0] + 1; j++) {
        printf("%d", Node[i][j]);
        if (j != Node[i][0])
          printf(", ");
      }
      printf("]\\n");
    }
  }
  return 0;
}

int getRootNode() {
  int i;
  for (i = 0; i < n; i++) {
    if (getParent(i) == -1)
      return i;
  }
  return i;
}

int getParent(int node) {
  int i, j;
  for (i = 0; i < n; i++) {
    for (j = 1; j < Node[i][0] + 1; j++) {
      if (Node[i][j] == node) {
        return i;
      }
    }
  }
  return -1;
}

int getDepth(int node) {
  int depth = 0;
  if (getParent(node) == -1)
    return depth;
  return getDepth(getParent(node)) + 1;
}
Problem p02279 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int a, b, c, d, p[100001], n, k;
  vector<int> e[100001];
  cin >> a;
  for (int i = 0; i <= a; i++)
    p[i] = -1;
  for (int i = 0; i < a; i++) {
    cin >> b >> c;
    for (int j = 0; j < c; j++) {
      cin >> d;
      p[d] = b;
      e[b].push_back(d);
    }
  }
  // cout<<"ttttttt";///////
  for (int i = 0; i < a; i++) {
    n = i;
    k = 0;
    while (p[n] != -1) {
      n = p[n];
      k++;
    }
    cout << "node " << i << ": parent = " << p[i];
    cout << ", depth = " << k;
    if (p[i] == -1)
      cout << ", root, [";
    else if (e[i].size() == 0)
      cout << ", leaf, [";
    else
      cout << ", internal node, [";

    for (int j = 0; j < e[i].size(); j++) {
      cout << e[i][j];
      if (e[i].size() - j == 1)
        ;
      else
        cout << ", ";
    }
    cout << "]\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int a, b, c, d, p[100001] = {-1}, n, k;
  vector<int> e[100001];
  cin >> a;
  for (int i = 0; i < a; i++) {
    cin >> b >> c;
    for (int j = 0; j < c; j++) {
      cin >> d;
      p[d] = b;
      e[b].push_back(d);
    }
  }
  // cout<<"ttttttt";///////
  for (int i = 0; i < a; i++) {
    n = i;
    k = 0;
    while (p[n] != -1) {
      n = p[n];
      k++;
    }
    cout << "node " << i << ": parent = " << p[i];
    cout << ", depth = " << k;
    if (p[i] == -1)
      cout << ", root, [";
    else if (e[i].size() == 0)
      cout << ", leaf, [";
    else
      cout << ", internal node, [";

    for (int j = 0; j < e[i].size(); j++) {
      cout << e[i][j];
      if (e[i].size() - j == 1)
        ;
      else
        cout << ", ";
    }
    cout << "]\\n";
  }
  return 0;
}
Problem p02279 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <limits>
#include <map>
#include <set>
#include <stdio.h>
#include <vector>
using namespace std;
map<int, int> mpa, mpb;
#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;
}

int 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;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <limits>
#include <map>
#include <set>
#include <stdio.h>
#include <vector>
using namespace std;
map<int, int> mpa, mpb;
#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;
}

int 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 < v; 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;
}
Problem p02279 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <cstdio>

using namespace std;

const int32_t N = 100000;
array<int32_t, N> parent, depth;
array<vector<int32_t>, N> children;

void calc_depth(int32_t id, int32_t d) {
  depth[id] = d;
  for (int32_t id_c : children[id])
    calc_depth(id_c, d + 1);
  return;
}

int main() {
  parent.fill(-1);
  int32_t n;

  scanf("%d", &n);
  for (int32_t i = 0; i < n; i++) {
    int32_t id;
    scanf("%d", &id);

    int32_t k;
    scanf("%d", &k);
    for (int32_t j = 0; j < k; j++) {
      int32_t c;
      scanf("%d", &c);
      parent[c] = id;
      children[id].push_back(c);
    }
  }

  int32_t id = 0;
  while (parent[id] != -1)
    id++;
  calc_depth(id, 0);

  for (int32_t id = 0; id < n; id++) {
    printf("node %d: parent = %d, depth = %d, ", id, parent[id], depth[id]);

    if (parent[id] == -1) {
      printf("root");
    } else if (children[id].empty()) {
      printf("leaf");
    } else {
      printf("internal node");
    }

    printf(", [");
    for (int32_t id_c : children[id]) {
      if (id_c != children[id][0])
        printf(", ");
      printf("%d", id_c);
    }
    printf("]\\n");
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int32_t depth(int32_t id, const unordered_map<int32_t, int32_t> &parent,
              unordered_map<int32_t, int32_t> &memo) {
  auto itr_m = memo.find(id);
  if (itr_m != memo.end()) {
    return itr_m->second;
  }

  int32_t d;
  auto itr_p = parent.find(id);
  if (itr_p == parent.end()) {
    d = 0;
  } else {
    d = depth(itr_p->second, parent, memo) + 1;
  }
  memo[id] = d;
  return d;
}

int main() {
  int32_t n;

  scanf("%d", &n);
  vector<int32_t> ids(n);
  unordered_map<int32_t, int32_t> parent;
  unordered_map<int32_t, string> children;
  for (int32_t i = 0; i < n; i++) {
    scanf("%d", &ids[i]);
    children[ids[i]] = "";

    int32_t k;
    scanf("%d", &k);
    for (int32_t j = 0; j < k; j++) {
      int32_t c;
      scanf("%d", &c);
      parent[c] = ids[i];
      if (children[ids[i]] != "") {
        children[ids[i]] = children[ids[i]] + ", " + to_string(c);
      } else {
        children[ids[i]] = to_string(c);
      }
    }
  }

  sort(ids.begin(), ids.end());

  unordered_map<int32_t, int32_t> memo;
  for (int32_t id : ids) {
    printf("node %d: parent = ", id);

    auto itr = parent.find(id);
    if (itr != parent.end()) {
      printf("%d", itr->second);
    } else {
      printf("-1");
    }

    printf(", depth = %d, ", depth(id, parent, memo));

    if (itr == parent.end()) {
      printf("root");
    } else if (children[id] == "") {
      printf("leaf");
    } else {
      printf("internal node");
    }

    printf(", [%s]\\n", children[id].c_str());
  }

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

#include <stdio.h>
#define MAX 100005
#define OUT -1
struct Node {
  int p;
  int l;
  int r;
};

void depth(int, int);
void print(int);
// using namespace std;

struct Node T[MAX];
int n, A[MAX];

int main() {
  int i, j, d, v, c, l, r;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    T[i].r = OUT;
    T[i].l = T[i].r;
    T[i].p = T[i].l;
  }
  for (i = 0; i < n; i++) {
    scanf("%d", &v);
    scanf("%d", &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 == OUT) {
      r = i;
    }
  }
  depth(r, 0);
  for (i = 0; i < n; i++) {
    print(i);
  }
  return 0;
} // end of main func

void depth(int u, int p) {
  A[u] = p;
  if (T[u].r != OUT) {
    depth(T[u].r, p);
  }
  if (T[u].l != OUT) {
    depth(T[u].l, p + 1);
  }
} // end of depth func

void print(int u) {
  int i, c;
  printf("node %d: ", u);
  printf("parent = %d, ", T[u].p);
  printf("depth = %d, ", A[u]);
  if (T[u].p == OUT) {
    printf("root, ");
  } else if (T[u].l == OUT) {
    printf("leaf, ");
  } else {
    printf("internal node, ");
  }
  printf("[");
  for (i = 0, c = T[u].l; c != OUT; i++, c = T[c].r) {
    if (0 < i) {
      printf(", ");
    }
    printf("%d", c);
  }
  printf("]\\n");
} // end of print func

Error: WA

Fixed submission:

#include <iostream>
#define MAX 100005
#define OUT -1
struct Node {
  int p;
  int l;
  int r;
};

void depth(int, int);
void print(int);
using namespace std;

struct Node T[MAX];
int n, A[MAX];

int main() {
  int i, j, d, v, c, l, r;
  // scanf("%d",&n);
  cin >> n;
  for (i = 0; i < d; i++) {
    T[i].r = OUT;
    T[i].l = T[i].r;
    T[i].p = T[i].l;
  }
  for (i = 0; i < n; i++) {
    // scanf("%d",&v);
    // scanf("%d",&d);
    cin >> v;
    cin >> d;
  }
  for (j = 0; j < d; j++) {
    // scanf("%d",&c);
    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 == OUT) {
      r = i;
    }
  }
  depth(r, 0);
  for (i = 0; i < n; i++) {
    print(i);
  }
  return 0;
} // end of main func

void depth(int u, int p) {
  A[u] = p;
  if (T[u].r != OUT) {
    depth(T[u].r, p);
  }
  if (T[u].l != OUT) {
    depth(T[u].l, p + 1);
  }
} // end of depth func

void print(int u) {
  int i, c;
  // printf("node %d : ",u);
  cout << "node " << u << ": ";
  // printf("parent = %d, ",T[u].p);
  cout << "parent" << T[u].p << ", ";
  // printf("depth = %d , ",A[u]);
  cout << "depth = " << A[u] << " , ";
  if (T[u].p == OUT) {
    // printf("root :");
    cout << "root :";
  } else if (T[u].l == OUT) {
    // printf("leaf, ");
    cout << "leaf, ";
  } else {
    // printf("internal node ");
    cout << "internal node";
  }
  // printf("[");
  cout << "[";
  for (i = 0, c = T[u].l; c != OUT; i++, c = T[c].r) {
    if (0 < i) {
      // printf(", ");
      cout << ", ";
    }
    // printf("%d",c);
    cout << c;
  }
  // printf("]\\n");
  cout << "]" << endl;
} // end of print func
Problem p02279 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

#define MAX 100005
#define NIL -1

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

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

void print(int u) {
  int i, 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 (i = 0, c = T[u].l; c != NIL; i++, c = T[c].r) {
    if (i)
      printf(", ");
    printf("%d", c);
  }
  printf("]\\n");
}

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;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    T[i].p = T[i].l = T[i].r = 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 (i = 0; i < n; i++)
    print(i);
  return 0;
}


Error: 0

Fixed submission:

#include <stdio.h>

#define MAX 10000
#define NIL -1

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

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

void print(int u) {
  int i, 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 (i = 0, c = T[u].l; c != NIL; i++, c = T[c].r) {
    if (i)
      printf(", ");
    printf("%d", c);
  }
  printf("]\\n");
}

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;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    T[i].p = T[i].l = T[i].r = 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 (i = 0; i < n; i++)
    print(i);
  return 0;
}
Problem p02279 (C++) — buggy submission, verdict: Runtime Error

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

int n;

struct node {
  int id, k, d, pid;
  int *cid;
  string type;
} N[100005];

void GetDepth(node &GN) {
  GN.d = 0;
  node temp = GN;

  while (temp.pid != -1) {
    GN.d++;
    temp = N[temp.pid];
  }
}

void GetType(node &GN) {
  if (GN.pid == -1)
    GN.type = "root";
  else if (GN.k == 0)
    GN.type = "leaf";
  else
    GN.type = "internal node";
}

void print() {
  for (int i = 0; i < n; i++) {
    cout << "node " << N[i].id << ": "
         << "parent = " << N[i].pid << ", "
         << "depth = " << N[i].d << ", " << N[i].type << ", "
         << "[";

    if (N[i].k != 0) {
      for (int j = 0; j < N[i].k - 1; j++)
        cout << N[i].cid[j] << ", ";
      cout << N[i].cid[N[i].k - 1];
    }

    cout << "]" << endl;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int place;
  cin >> n;

  for (int i = 0; i < n; i++)
    N[i].pid = -1;

  for (int i = 0; i < n; i++) {
    cin >> place;
    N[place].id = place;
    cin >> N[place].k;

    if (N[place].k > 0)
      N[place].cid = (int *)malloc(sizeof(int) * N[place].k);

    for (int j = 0; j < N[place].k; j++) {
      cin >> N[place].cid[j];
      for (int t = 0; t < n; t++) {
        if (N[place].cid[j] == t) {
          N[t].pid = N[place].id;
          break;
        }
      }
    }
  }

  for (int i = 0; i < n; i++)
    GetDepth(N[i]);

  for (int i = 0; i < n; i++)
    GetType(N[i]);

  print();

  return 0;
}


Error: 0

Fixed submission:

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

int n;

struct node {
  int id, k, d, pid;
  int *cid;
  string type;
} N[100005];

void GetDepth(node &GN) {
  GN.d = 0;
  node temp = GN;

  while (temp.pid != -1) {
    GN.d++;
    temp = N[temp.pid];
  }
}

void GetType(node &GN) {
  if (GN.pid == -1)
    GN.type = "root";
  else if (GN.k == 0)
    GN.type = "leaf";
  else
    GN.type = "internal node";
}

void print() {
  for (int i = 0; i < n; i++) {
    cout << "node " << N[i].id << ": "
         << "parent = " << N[i].pid << ", "
         << "depth = " << N[i].d << ", " << N[i].type << ", "
         << "[";

    if (N[i].k != 0) {
      for (int j = 0; j < N[i].k - 1; j++)
        cout << N[i].cid[j] << ", ";
      cout << N[i].cid[N[i].k - 1];
    }

    cout << "]" << endl;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int place;
  cin >> n;

  for (int i = 0; i < n; i++) {
    N[i].pid = -1;
    N[i].cid = (int *)malloc(sizeof(int) * 100005);
  }

  for (int i = 0; i < n; i++) {
    cin >> place;
    N[place].id = place;
    cin >> N[place].k;

    for (int j = 0; j < N[place].k; j++) {
      cin >> N[place].cid[j];
      for (int t = 0; t < n; t++) {
        if (N[place].cid[j] == t) {
          N[t].pid = N[place].id;
          break;
        }
      }
    }
  }

  for (int i = 0; i < n; i++)
    GetDepth(N[i]);

  for (int i = 0; i < n; i++)
    GetType(N[i]);

  print();

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

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

int n;

const int NIL = -1;
struct Node {
  int parent, left, right;
};
vector<Node> T;

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

void printChildren(int u) {
  int c = T[u].left;
  int times = 0;
  while (c != NIL) {
    if (times != 0)
      cout << ", ";
    cout << c;
    c = T[c].right;
    ++times;
  }
}

string getType(int u) {
  if (T[u].parent == NIL)
    return "root";
  else if (T[u].left != NIL)
    return "internal node";
  else
    return "leaf";
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  T.reserve(n);
  for (int i = 0; i < n; ++i) {
    T[i].parent = NIL;
    T[i].left = NIL;
    T[i].right = NIL;
  }
  for (int i = 0; i < n; ++i) {
    int id;
    cin >> id;
    int k;
    cin >> k;
    int prev_child;
    for (int j = 0; j < k; ++j) {
      int child;
      cin >> child;
      if (j == 0)
        T[id].left = child;
      T[child].parent = id;
      if (j != 0)
        T[prev_child].right = child;
      prev_child = child;
    }
  }

  for (int i = 0; i < n; ++i) {
    cout << "node " << i << ": parent = " << T[i].parent
         << ", depth = " << getDepth(i) << ", " << getType(i) << ", [";
    printChildren(i);
    cout << "]" << endl;
  }
}


Error: TLE

Fixed submission:

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

int n;

const int NIL = -1;
struct Node {
  int parent, left, right;
};
vector<Node> T;

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

void printChildren(int u) {
  int c = T[u].left;
  int times = 0;
  while (c != NIL) {
    if (times != 0)
      cout << ", ";
    cout << c;
    c = T[c].right;
    ++times;
  }
}

string getType(int u) {
  if (T[u].parent == NIL)
    return "root";
  else if (T[u].left != NIL)
    return "internal node";
  else
    return "leaf";
}

int main() {
  cin >> n;
  T.reserve(n);
  for (int i = 0; i < n; ++i) {
    T[i].parent = NIL;
    T[i].left = NIL;
    T[i].right = NIL;
  }
  for (int i = 0; i < n; ++i) {
    cin.ignore(numeric_limits<std::streamsize>::max(), ' ');
    int k;
    cin >> k;
    int prev_child;
    for (int j = 0; j < k; ++j) {
      int child;
      cin >> child;
      if (j == 0)
        T[i].left = child;
      T[child].parent = i;
      if (j != 0)
        T[prev_child].right = child;
      prev_child = child;
    }
  }

  for (int i = 0; i < n; ++i) {
    cout << "node " << i << ": parent = " << T[i].parent
         << ", depth = " << getDepth(i) << ", " << getType(i) << ", [";
    printChildren(i);
    cout << "]" << endl;
  }
}