prithivMLmods/Coder-Stat huggingface dataset:
Problem p02276 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int *A = new int[n + 1];
  for (int i = 1; i < n + 1; i++) {
    cin >> A[i];
  }

  int x = A[n];
  int i = 0;
  int tmp = 0;
  for (int j = 1; j <= n - 1; j++) {
    if (A[j] <= x) {
      i = i + 1;
      tmp = A[i];
      A[i] = A[j];
      A[j] = tmp;
    }
  }
  tmp = A[i + 1];
  A[i + 1] = A[n];
  A[n] = tmp;
  i = i + 1;

  for (int j = 1; j < n + 1; j++) {
    if (j == i) {
      cout << '[' << A[j] << ']';
    } else {
      cout << A[j];
    }
    if (j = n - 1) {
      cout << ' ';
    } else {
      cout << endl;
    }
  }
  delete[] A;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int *A = new int[n + 1];
  for (int i = 1; i < n + 1; i++) {
    cin >> A[i];
  }

  int x = A[n];
  int i = 0;
  int tmp = 0;
  for (int j = 1; j <= n - 1; j++) {
    if (A[j] <= x) {
      i = i + 1;
      tmp = A[i];
      A[i] = A[j];
      A[j] = tmp;
    }
  }
  tmp = A[i + 1];
  A[i + 1] = A[n];
  A[n] = tmp;
  i = i + 1;

  for (int j = 1; j < n + 1; j++) {
    if (j == i) {
      cout << '[' << A[j] << ']';
    } else {
      cout << A[j];
    }
    if (j < n) {
      cout << ' ';
    } else {
      cout << endl;
    }
  }
  delete[] A;
  return 0;
}
Problem p02276 (C++)

Buggy submission (status: Runtime Error):
#include <cstdlib>
#include <iostream>
#include <list>
#include <stack>
#include <string>
using namespace std;
int main() {
  int n, s[10000], small = 0, large = 0, cnt = 0;
  cin >> n;
  bool b = true;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < n; i++) {
    if (s[i] <= s[n - 1]) {
      small = i;
      cnt++;
      if (small > large) {
        swap(s[small], s[large]);
        large += 1;
      }
    } else if (b == true) {
      b = false;
      large = i;
    }
    if (b == true) {
      large++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (i == cnt - 1) {
      printf(" [%d]", s[i]);
    } else {
      if (i != 0) {
        cout << " ";
      }
      cout << s[i];
    }
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <cstdlib>
#include <iostream>
#include <list>
#include <stack>
#include <string>
using namespace std;
int main() {
  int n, s[100000], small = 0, large = 0, cnt = 0;
  cin >> n;
  bool b = true;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < n; i++) {
    if (s[i] <= s[n - 1]) {
      small = i;
      cnt++;
      if (small > large) {
        swap(s[small], s[large]);
        large += 1;
      }
    } else if (b == true) {
      b = false;
      large = i;
    }
    if (b == true) {
      large++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (i == cnt - 1) {
      printf(" [%d]", s[i]);
    } else {
      if (i != 0) {
        cout << " ";
      }
      cout << s[i];
    }
  }
  cout << endl;
  return 0;
}
Problem p02277 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define AMAX 100001

struct card {
  char mark;
  int value;
  int cnt;
};

card a[AMAX];
int partion(int q, int r) {
  int x = a[r].value;
  int i = q - 1;
  for (int j = q; j < r; j++) {
    if (a[j].value <= x) {
      i = i + 1;
      swap(a[i], a[j]);
    }
  }
  swap(a[i + 1], a[r]);
  return i + 1;
}

void quicksort(int p, int r) {

  if (p < r) {
    int q;
    q = partion(q, r);
    quicksort(p, q - 1);
    quicksort(q + 1, r);
  }
}

int main() {

  int n;
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf(" %c %d", &a[i].mark, &a[i].value);
    a[i].cnt = i;
  }

  quicksort(0, n - 1);
  int junge = 0;

  for (int i = 1; i < n; i++) {
    if (a[i - 1].value == a[i].value && a[i - 1].cnt > a[i].cnt)
      junge = 1;
  }
  if (junge == 1)
    printf("Not stable\n");
  else
    printf("Stable\n");

  for (int i = 0; i < n; i++) {
    printf("%c %d\n", a[i].mark, a[i].value);
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define AMAX 100001

struct card {
  char mark;
  int value;
  int cnt;
};

card a[AMAX];
int partion(int q, int r) {
  int x = a[r].value;
  int i = q - 1;
  for (int j = q; j < r; j++) {
    if (a[j].value <= x) {
      i = i + 1;
      swap(a[i], a[j]);
    }
  }
  swap(a[i + 1], a[r]);
  return i + 1;
}

void quicksort(int p, int r) {

  if (p < r) {
    int q;
    q = partion(p, r);
    quicksort(p, q - 1);
    quicksort(q + 1, r);
  }
}

int main() {

  int n;
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf(" %c %d", &a[i].mark, &a[i].value);
    a[i].cnt = i;
  }

  quicksort(0, n - 1);
  int junge = 0;

  for (int i = 1; i < n; i++) {
    if (a[i - 1].value == a[i].value && a[i - 1].cnt > a[i].cnt)
      junge = 1;
  }
  if (junge == 1)
    printf("Not stable\n");
  else
    printf("Stable\n");

  for (int i = 0; i < n; i++) {
    printf("%c %d\n", a[i].mark, a[i].value);
  }
}
Problem p02278 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
#define MAX_N 10000
#define MAX_V 65000000
#define INF 10000000000

int n, A[MAX_N], s, B[MAX_N], T[MAX_V], cur, S, m, x;
bool V[MAX_N];

int minimum_cost_sort() {
  int ans = 0;
  for (int i = 0; i < MAX_V; i++) {
    V[i] = false;
  }
  for (int i = 0; i < n; i++) {
    B[i] = A[i];
  }
  sort(B, B + n);
  for (int i = 0; i < n; i++) {
    T[B[i]] = i;
  }
  for (int i = 0; i < n; i++) {
    if (V[i] == true) {
      goto owata;
    }
    cur = i;
    S = 0;
    m = INF;
    x = 0;
    while (1) {
      V[cur] = true;
      x++;
      int v = A[cur];
      m = min(m, v);
      S += v;
      cur = T[v];
      if (V[cur]) {
        break;
      }
    }
    ans += min(S + (x - 2) * m, m + S + (x + 1) * s);
  owata:;
  }
  return ans;
}

int main() {
  cin >> n;
  s = INF;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    s = min(s, A[i]);
  }
  cout << minimum_cost_sort() << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
#define MAX_N 10000
#define MAX_V 60000000
#define INF 10000000000

int n, A[MAX_N], s, B[MAX_N], T[MAX_V], cur, S, m, x;
bool V[MAX_N];

int minimum_cost_sort() {
  int ans = 0;
  for (int i = 0; i < MAX_V; i++) {
    V[i] = false;
  }
  for (int i = 0; i < n; i++) {
    B[i] = A[i];
  }
  sort(B, B + n);
  for (int i = 0; i < n; i++) {
    T[B[i]] = i;
  }
  for (int i = 0; i < n; i++) {
    if (V[i] == true) {
      goto owata;
    }
    cur = i;
    S = 0;
    m = INF;
    x = 0;
    while (1) {
      V[cur] = true;
      x++;
      int v = A[cur];
      m = min(m, v);
      S += v;
      cur = T[v];
      if (V[cur]) {
        break;
      }
    }
    ans += min(S + (x - 2) * m, m + S + (x + 1) * s);
  owata:;
  }
  return ans;
}

int main() {
  cin >> n;
  s = INF;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    s = min(s, A[i]);
  }
  cout << minimum_cost_sort() << endl;
  return 0;
}
Problem p02278 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

int n, w[1000], s;
int a[1000], d[1001]; // destination

long solve() {
  long ret = 0;

  bool v[1000];
  for (int i = 0; i < n; ++i) {
    a[i] = w[i];
    v[i] = false;
  }
  sort(a, a + n); // a[]??????w[]???????????¢?????\???

  //??????a[i]??????????????????????????´??????d[]?????\??????
  for (int i = 0; i < n; ++i)
    d[a[i]] = i;

  for (int i = 0; i < n; ++i) {
    if (v[i])
      continue; //??¢???????????????????????????????????????????????§???????????????????????£?????????

    //?????????????????¢???
    int cur = i; //?????¨??????
    int sum = 0;
    int m = 10000; //???????????????????????????????°????
    int an = 0;    //?????????????????§??????

    while (1) {
      v[cur] = true; //?¨??????????????¨????
      an++;
      int z = w[cur];
      sum += z;
      m = min(m, z);
      cur = d[z]; //?¬??????????
      if (v[cur])
        break; //?????¨??????
    }

    ret += min(sum + (an - 2) * m, m + sum + (an + 1) * s);
  }
  return ret;
}

int main() {
  scanf(" %d", &n);

  s = 10000;
  for (int i = 0; i < n; ++i) {
    scanf(" %d", &w[i]);
    s = min(s, w[i]); // s???w[]?????????????°????????´?
  }

  printf("%ld\n", solve());
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

int n, w[1000], s;
int a[1000], d[10001]; // destination

long solve() {
  long ret = 0;

  bool v[1000];
  for (int i = 0; i < n; ++i) {
    a[i] = w[i];
    v[i] = false;
  }
  sort(a, a + n); // a[]??????w[]???????????¢?????\???

  //??????a[i]??????????????????????????´??????d[]?????\??????
  for (int i = 0; i < n; ++i)
    d[a[i]] = i;

  for (int i = 0; i < n; ++i) {
    if (v[i])
      continue; //??¢???????????????????????????????????????????????§???????????????????????£?????????

    //?????????????????¢???
    int cur = i; //?????¨??????
    int sum = 0;
    int m = 10000; //???????????????????????????????°????
    int an = 0;    //?????????????????§??????

    while (1) {
      v[cur] = true; //?¨??????????????¨????
      an++;
      int z = w[cur];
      sum += z;
      m = min(m, z);
      cur = d[z]; //?¬??????????
      if (v[cur])
        break; //?????¨??????
    }

    ret += min(sum + (an - 2) * m, m + sum + (an + 1) * s);
  }
  return ret;
}

int main() {
  scanf(" %d", &n);

  s = 10000;
  for (int i = 0; i < n; ++i) {
    scanf(" %d", &w[i]);
    s = min(s, w[i]); // s???w[]?????????????°????????´?
  }

  printf("%ld\n", solve());
}
Problem p02278 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;
static const int MAX = 1000;
static const int VMAX = 10000;

int n, A[MAX], s;
int B[MAX], T[VMAX + 1];

int solve() {
  int ans = 0;

  bool V[MAX];
  for (int i = 0; i < n; i++) {
    B[i] = A[i];
    V[i] = false;
  }
  sort(B, B + n);
  for (int i = 0; i < n; i++)
    T[B[i]] = i;
  for (int i = 0; i < n; i++) {
    if (V[i])
      continue;
    int cur = i;
    int S = 0;
    int m = VMAX;
    int an = 0;
    do {
      V[cur] = true;
      an++;
      int v = A[cur];
      m = min(m, v);
      S += v;
      cur = T[v];
    } while (V[cur]);
    ans += min(S + (an - 2) * m, m + S + (an + 1) * S);
  }
  return ans;
}

int main() {
  cin >> n;
  s = VMAX;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    s = min(s, A[i]);
  }
  int ans = solve();
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
static const int MAX = 1000;
static const int VMAX = 10000;

int n, A[MAX], s;
int B[MAX], T[VMAX + 1];

int solve() {
  int ans = 0;

  bool V[MAX];
  for (int i = 0; i < n; i++) {
    B[i] = A[i];
    V[i] = false;
  }
  sort(B, B + n);
  for (int i = 0; i < n; i++)
    T[B[i]] = i;
  for (int i = 0; i < n; i++) {
    if (V[i])
      continue;
    int cur = i;
    int S = 0;
    int m = VMAX;
    int an = 0;
    do {
      V[cur] = true;
      an++;
      int v = A[cur];
      m = min(m, v);
      S += v;
      cur = T[v];
    } while (!V[cur]);
    ans += min(S + (an - 2) * m, m + S + (an + 1) * s);
  }
  return ans;
}

int main() {
  cin >> n;
  s = VMAX;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    s = min(s, A[i]);
  }
  int ans = solve();
  cout << ans << endl;
}
Problem p02278 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define N 1005
#define INF (1 << 30)
typedef long long ll;
using namespace std;

int main() {
  int weight[N];
  int sorted[N];
  int idx[N];
  int n, x = INF, ans = 0;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    scanf("%d", weight + i);
    sorted[i] = weight[i];
    if (x > weight[i])
      x = weight[i];
  }
  sort(sorted, sorted + n);
  for (int i = 0; i < n; ++i)
    idx[sorted[i]] = i;
  bool visited[N]{false};
  for (int i = 0; i < n; ++i) {
    if (visited[i] || i == idx[weight[i]])
      continue;
    int Min = weight[i], num = 1, sum1 = weight[i];
    int j = i;
    while (true) {
      visited[j] = true;
      j = idx[weight[j]];
      if (visited[j])
        break;
      Min = min(Min, weight[j]);
      sum1 += weight[j];
      ++num;
    }
    ans += min(Min + (num + 1) * x, (num - 2) * Min) + sum1;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define N 1005
#define INF (1 << 30)
typedef long long ll;
using namespace std;

int main() {
  int weight[N];
  int sorted[N];
  int idx[40005];
  int n, x = INF, ans = 0;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    scanf("%d", weight + i);
    sorted[i] = weight[i];
    if (x > weight[i])
      x = weight[i];
  }
  sort(sorted, sorted + n);
  for (int i = 0; i < n; ++i)
    idx[sorted[i]] = i;
  bool visited[N]{false};
  for (int i = 0; i < n; ++i) {
    if (visited[i] || i == idx[weight[i]])
      continue;
    int Min = weight[i], num = 1, sum1 = weight[i];
    int j = i;
    while (true) {
      visited[j] = true;
      j = idx[weight[j]];
      if (visited[j])
        break;
      Min = min(Min, weight[j]);
      sum1 += weight[j];
      ++num;
    }
    ans += min(Min + (num + 1) * x, (num - 2) * Min) + sum1;
  }
  cout << ans << endl;
  return 0;
}
Problem p02278 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>
#define MAX 100000
using namespace std;
typedef long long ll;
ll ans;
void solve(int a[], int n) {
  int b[1000], t[1000], vmin, vmax = 1000;
  bool vis[1000];
  for (int i = 0; i < n; i++) {
    b[i] = a[i];
    vis[i] = false;
  }
  sort(b, b + n);
  vmin = b[0];
  for (int i = 0; i < n; i++) {
    t[b[i]] = i; // 得出移动够各个元素（值不同）应该在数组中位置
  }
  for (int i = 0; i < n; i++) {
    int cur = i, m = vmax, sum = 0, num = 0; // 查找新的环
    while (1)                                // 开始搜索下一个环
    {
      vis[cur] = true;    // 将这个元素加入当前环
      num++;              // 这个环的个数加1
      sum += a[cur];      // 环的weight加1
      m = min(a[cur], m); // 更新最小值，最终得到该环的最小值
      cur = t[a[cur]];    // 得到将要移动到的位置
      if (vis[cur])
        break; // 将要移动到的位置的元素已经在环中，环已经闭合，一个环已经找到
    }
    ans += min(
        sum + (num - 2) * m,
        sum + m +
            (num + 1) *
                vmin); // 如果已经在某个环中或者根本不需要移动，那么成本为0，不影响结果
  }
}
int main() {
  int n, a[1000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  solve(a, n);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>
#define MAX 100000
using namespace std;
typedef long long ll;
ll ans;
void solve(int a[], int n) {
  int b[1000], t[10005], vmin, vmax = 10000;
  bool vis[1000];
  for (int i = 0; i < n; i++) {
    b[i] = a[i];
    vis[i] = false;
  }
  sort(b, b + n);
  vmin = b[0];
  for (int i = 0; i < n; i++) {
    t[b[i]] = i; // 得出移动够各个元素（值不同）应该在数组中位置
  }
  for (int i = 0; i < n; i++) {
    int cur = i, m = vmax, sum = 0, num = 0; // 查找新的环
    while (1)                                // 开始搜索下一个环
    {
      vis[cur] = true;    // 将这个元素加入当前环
      num++;              // 这个环的个数加1
      sum += a[cur];      // 环的weight加1
      m = min(a[cur], m); // 更新最小值，最终得到该环的最小值
      cur = t[a[cur]];    // 得到将要移动到的位置
      if (vis[cur])
        break; // 将要移动到的位置的元素已经在环中，环已经闭合，一个环已经找到
    }
    ans += min(
        sum + (num - 2) * m,
        sum + m +
            (num + 1) *
                vmin); // 如果已经在某个环中或者根本不需要移动，那么成本为0，不影响结果
  }
}
int main() {
  int n, a[1000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  solve(a, n);
  cout << ans << endl;
  return 0;
}
Problem p02278 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int minimumCostSort(int *W, int n) {
  int X[1000], R[1000];
  for (int i = 0; i < n; i++) {
    X[i] = W[i];
  }
  sort(X, X + n);
  for (int i = 0; i < n; i++)
    R[X[i]] = i;
  int m = X[0];
  int sum = 0;

  for (int i = 0; i < n; i++) {
    int cur = i;
    if (X[cur] == -1)
      continue;
    int s = 0;
    int mm = 10000;
    int nn = 0;
    int v;
    while (true) {
      nn++;
      X[cur] = -1;
      v = W[cur];
      mm = min(v, mm);
      s += v;
      cur = R[v];
      if (X[cur] == -1)
        break;
    }
    sum += min(s + (nn - 2) * mm, s + mm + (nn + 1) * m);
  }

  return sum;
}

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

  int n;
  int W[1000];

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> W[i];
  }

  int c = minimumCostSort(W, n);

  cout << c << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int minimumCostSort(int *W, int n) {
  int X[1000], R[10001];
  for (int i = 0; i < n; i++) {
    X[i] = W[i];
  }
  sort(X, X + n);
  for (int i = 0; i < n; i++)
    R[X[i]] = i;
  int m = X[0];
  int sum = 0;

  for (int i = 0; i < n; i++) {
    int cur = i;
    if (X[cur] == -1)
      continue;
    int s = 0;
    int mm = 10000;
    int nn = 0;
    int v;
    while (true) {
      nn++;
      X[cur] = -1;
      v = W[cur];
      mm = min(v, mm);
      s += v;
      cur = R[v];
      if (X[cur] == -1)
        break;
    }
    sum += min(s + (nn - 2) * mm, s + mm + (nn + 1) * m);
  }

  return sum;
}

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

  int n;
  int W[1000];

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> W[i];
  }

  int c = minimumCostSort(W, n);

  cout << c << endl;

  return 0;
}
Problem p02278 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 200005;

int w[1005], B[1005], L[1005], flag[1005];

int main() {
  int n, ans = 0, Min = INF;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> w[i];
    B[i] = w[i];
    Min = min(Min, w[i]);
  }
  sort(B, B + n);
  for (int i = 0; i < n; i++)
    L[B[i]] = i;
  for (int i = 0; i < n; i++) {
    if (flag[i])
      continue;
    int temp = i, S = INF, m = 0, cnt = 0;
    while (!flag[temp]) {
      cnt++;
      flag[temp] = 1;
      S = min(S, w[temp]);
      m += w[temp];
      temp = L[w[temp]];
    }
    ans += min(m + (cnt - 2) * S, m + (cnt + 1) * Min + S);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 200005;

int w[1005], B[1005], L[100005], flag[1005];

int main() {
  int n, ans = 0, Min = INF;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> w[i];
    B[i] = w[i];
    Min = min(Min, w[i]);
  }
  sort(B, B + n);
  for (int i = 0; i < n; i++)
    L[B[i]] = i;
  for (int i = 0; i < n; i++) {
    if (flag[i])
      continue;
    int temp = i, S = INF, m = 0, cnt = 0;
    while (!flag[temp]) {
      cnt++;
      flag[temp] = 1;
      S = min(S, w[temp]);
      m += w[temp];
      temp = L[w[temp]];
    }
    ans += min(m + (cnt - 2) * S, m + (cnt + 1) * Min + S);
  }
  cout << ans << endl;
  return 0;
}
Problem p02279 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
/*#include<iostream>
#include<algorithm>
#include<climits>*/
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define rep1(i, a, b) for (int i = a; i <= (b); ++i)
#define per(i, a, b) for (int i = a; i > (b); --i)
#define per1(i, a, b) for (int i = a; i >= (b); --i)
#define LL long long
#define inf 0x7f7f7f7f
#define N 1010
using namespace std;
struct node {
  int p, l, r;
};

node T[N];
int n, D[N];

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 == -1)
    printf("root, ");
  else if (T[u].l == -1)
    printf("leaf, ");
  else
    printf("internal node, ");

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

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

int main() {
  int i, j;
  int n, v, d, c, r, l;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    T[i].p = T[i].l = T[i].r = -1;
  }
  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 == -1)
      r = i;
  }
  rec(r, 0);

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


Fixed submission:
#include <bits/stdc++.h>
/*#include<iostream>
#include<algorithm>
#include<climits>*/
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define rep1(i, a, b) for (int i = a; i <= (b); ++i)
#define per(i, a, b) for (int i = a; i > (b); --i)
#define per1(i, a, b) for (int i = a; i >= (b); --i)
#define LL long long
#define inf 0x7f7f7f7f
#define N 100010
using namespace std;
struct node {
  int p, l, r;
};

node T[N];
int n, D[N];

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 == -1)
    printf("root, ");
  else if (T[u].l == -1)
    printf("leaf, ");
  else
    printf("internal node, ");

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

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

int main() {
  int i, j;
  int n, v, d, c, r, l;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    T[i].p = T[i].l = T[i].r = -1;
  }
  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 == -1)
      r = i;
  }
  rec(r, 0);

  for (i = 0; i < n; i++) {
    print(i);
  }
}
Problem p02279 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

struct node {
  int parent, depth;
  vector<int> child;
};

vector<node> tree;

void dfs(int num) {
  if (tree[num].child.size() == 0)
    return;
  for (int i = 0; i < tree[num].child.size(); i++) {
    tree[tree[num].child[i]].depth = tree[num].depth + 1;
    dfs(tree[num].child[i]);
  }
}

int main() {
  int n, m, now, t;
  cin >> n;
  tree.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> now >> m;
    vector<int> vec;
    for (int j = 0; j < m; j++) {
      cin >> t;
      vec.push_back(t);
    }
    tree[now] = {-1, 0, vec};
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < tree[i].child.size(); j++)
      tree[tree[i].child[j]].parent = i;
  }
  int par;
  for (int i = 0; i < n; i++) {
    if (tree[i].parent == -1)
      par = i;
  }
  dfs(par);
  for (int i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << tree[i].parent
         << ", depth = " << tree[i].depth;
    if (tree[i].parent == -1) {
      cout << ", root, [" << tree[i].child[0];
      for (int j = 1; j < tree[i].child.size(); j++)
        cout << ", " << tree[i].child[j];
      cout << "]" << endl;
    } else if (tree[i].child.size() == 0)
      cout << ", leaf, []" << endl;
    else {
      cout << ", internal node, [" << tree[i].child[0];
      for (int j = 1; j < tree[i].child.size(); j++)
        cout << ", " << tree[i].child[j];
      cout << "]" << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

struct node {
  int parent, depth;
  vector<int> child;
};

vector<node> tree;

void dfs(int num) {
  if (tree[num].child.size() == 0)
    return;
  for (int i = 0; i < tree[num].child.size(); i++) {
    tree[tree[num].child[i]].depth = tree[num].depth + 1;
    dfs(tree[num].child[i]);
  }
}

int main() {
  int n, m, now, t;
  cin >> n;
  tree.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> now >> m;
    vector<int> vec;
    for (int j = 0; j < m; j++) {
      cin >> t;
      vec.push_back(t);
    }
    tree[now] = {-1, 0, vec};
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < tree[i].child.size(); j++)
      tree[tree[i].child[j]].parent = i;
  }
  int par;
  for (int i = 0; i < n; i++) {
    if (tree[i].parent == -1)
      par = i;
  }
  dfs(par);
  for (int i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << tree[i].parent
         << ", depth = " << tree[i].depth;
    if (tree[i].parent == -1) {
      cout << ", root, [";
      if (tree[i].child.size() != 0)
        cout << tree[i].child[0];
      for (int j = 1; j < tree[i].child.size(); j++)
        cout << ", " << tree[i].child[j];
      cout << "]" << endl;
    } else if (tree[i].child.size() == 0)
      cout << ", leaf, []" << endl;
    else {
      cout << ", internal node, [" << tree[i].child[0];
      for (int j = 1; j < tree[i].child.size(); j++)
        cout << ", " << tree[i].child[j];
      cout << "]" << endl;
    }
  }
  return 0;
}
Problem p02279 (C++)

Buggy submission (status: 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;
}

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 (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

struct Node {
  string type;
  int depth;
  int parent = -1;
  vector<int> child;
};

int main() {
  int n;
  Node node[1000];
  queue<Node *> q;
  int depth = 0;

  cin >> n;
  for (int i = 0; i < n; i++) {
    int id, k;
    cin >> id >> k;
    for (int j = 0; j < k; j++) {
      int temp;
      cin >> temp;
      node[id].child.push_back(temp);
      node[temp].parent = id;
    }
  }

  for (int i = 0; i < n; i++) {
    if (node[i].parent == -1) {
      q.push(&node[i]);
      break;
    }
  }

  while (!q.empty()) {
    int size = q.size();
    for (int i = 0; i < size; i++) {

      if (q.front()->parent == -1) {
        q.front()->type = "root";
      } else if (q.front()->child.empty()) {
        q.front()->type = "leaf";
      } else {
        q.front()->type = "internal node";
      }

      for (int j = 0; j < q.front()->child.size(); j++) {
        q.push(&node[q.front()->child[j]]);
      }
      q.front()->depth = depth;
      q.pop();
    }
    depth++;
  }

  for (int i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << node[i].parent << ", ";
    cout << "depth = " << node[i].depth << ", ";
    cout << node[i].type << ", ";
    cout << "[";
    for (int j = 0; j < node[i].child.size(); j++) {
      cout << node[i].child[j];
      if (j < node[i].child.size() - 1) {
        cout << ", ";
      }
    }
    cout << "]" << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

struct Node {
  string type;
  int depth;
  int parent = -1;
  vector<int> child;
};

int main() {
  int n;
  Node node[100000];
  queue<Node *> q;
  int depth = 0;

  cin >> n;
  for (int i = 0; i < n; i++) {
    int id, k;
    cin >> id >> k;
    for (int j = 0; j < k; j++) {
      int temp;
      cin >> temp;
      node[id].child.push_back(temp);
      node[temp].parent = id;
    }
  }

  for (int i = 0; i < n; i++) {
    if (node[i].parent == -1) {
      q.push(&node[i]);
      break;
    }
  }

  while (!q.empty()) {
    int size = q.size();
    for (int i = 0; i < size; i++) {

      if (q.front()->parent == -1) {
        q.front()->type = "root";
      } else if (q.front()->child.empty()) {
        q.front()->type = "leaf";
      } else {
        q.front()->type = "internal node";
      }

      for (int j = 0; j < q.front()->child.size(); j++) {
        q.push(&node[q.front()->child[j]]);
      }
      q.front()->depth = depth;
      q.pop();
    }
    depth++;
  }

  for (int i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << node[i].parent << ", ";
    cout << "depth = " << node[i].depth << ", ";
    cout << node[i].type << ", ";
    cout << "[";
    for (int j = 0; j < node[i].child.size(); j++) {
      cout << node[i].child[j];
      if (j < node[i].child.size() - 1) {
        cout << ", ";
      }
    }
    cout << "]" << endl;
  }

  return 0;
}
Problem p02279 (C++)

Buggy submission (status: Runtime Error):
#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;
}


Fixed submission:
#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;
}
Problem p02279 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define N 100005
#define INF (1 << 30)
typedef long long ll;
using namespace std;

vector<int> child[N];
int parent[N];
int GetDepth(int idx) {
  int ret = 0;
  while (parent[idx] != -1) {
    ++ret;
    idx = parent[idx];
  }
  return ret;
}
const char *str[3] = {"root", "internal node", "leaf"};
int main() {
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
  int n;
  cin >> n;
  fill(parent, parent + n, -1);
  int id, k, c;
  for (int i = 0; i < n; ++i) {
    cin >> id >> k;
    for (int j = 0; j < k; ++j) {
      cin >> c;
      parent[c] = id;
      child[id].push_back(c);
    }
  }
  for (int i = 0; i < n; ++i) {
    int depth = GetDepth(i);
    int status;
    if (depth == 0)
      status = 0;
    else if (child[i].size()) {
      status = 1;
      sort(child[i].begin(), child[i].end());
    } else
      status = 2;
    printf("node %d: parent = %d, depth = %d, %s, ", i, parent[i], depth,
           str[status]);
    printf("[");
    for (int j = 0; j < child[i].size(); ++j) {
      if (j)
        printf(", ");
      printf("%d", child[i][j]);
    }
    printf("]\n");
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define N 100005
#define INF (1 << 30)
typedef long long ll;
using namespace std;

vector<int> child[N];
int parent[N];
int GetDepth(int idx) {
  int ret = 0;
  while (parent[idx] != -1) {
    ++ret;
    idx = parent[idx];
  }
  return ret;
}
const char *str[3] = {"root", "internal node", "leaf"};
int main() {
  int n;
  cin >> n;
  fill(parent, parent + n, -1);
  int id, k, c;
  for (int i = 0; i < n; ++i) {
    cin >> id >> k;
    for (int j = 0; j < k; ++j) {
      cin >> c;
      parent[c] = id;
      child[id].push_back(c);
    }
  }
  for (int i = 0; i < n; ++i) {
    int depth = GetDepth(i);
    int status;
    if (depth == 0)
      status = 0;
    else if (child[i].size()) {
      status = 1;
      sort(child[i].begin(), child[i].end());
    } else
      status = 2;
    printf("node %d: parent = %d, depth = %d, %s, ", i, parent[i], depth,
           str[status]);
    printf("[");
    for (int j = 0; j < child[i].size(); ++j) {
      if (j)
        printf(", ");
      printf("%d", child[i][j]);
    }
    printf("]\n");
  }
  return 0;
}
Problem p02279 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <stdlib.h>
#define N 10000

typedef struct node {
  int p;
  int right;
  int left;
} Node;

int main() {

  int n, i, j, l, c, k, id, P, depth, r;
  Node a[N], tmp;

  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    a[i].p = -1;
    a[i].right = -1;
    a[i].left = -1;
  }

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

    for (j = 0; j < k; j++) {
      scanf("%d", &c);
      if (j == 0)
        a[id].left = c;
      else
        a[r].right = c;
      a[c].p = id;
      r = c;
    }
  }

  for (i = 0; i < n; i++) {
    depth = 0;
    P = a[i].p;

    while (P != -1) {
      P = a[P].p;
      depth++;
    }

    if (a[i].p == -1) {
      printf("node %d: parent = %d, depth = %d, root, [", i, a[i].p, depth);
    } else if (a[i].left == -1)
      printf("node %d: parent = %d, depth = %d, leaf, [", i, a[i].p, depth);
    else
      printf("node %d: parent = %d, depth = %d, internal node, [", i, a[i].p,
             depth);

    P = a[i].left;
    if (P != -1)
      printf("%d", P);

    while (P != -1) {
      P = a[P].right;
      if (P != -1)
        printf(", %d", P);
    }
    printf("]\n");
  }

  return 0;
}

Fixed submission:
#include <stdio.h>
#include <stdlib.h>
#define N 1000000

typedef struct node {
  int p;
  int right;
  int left;
} Node;

int main() {

  int n, i, j, l, c, k, id, P, depth, r;
  Node a[N], tmp;

  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    a[i].p = -1;
    a[i].right = -1;
    a[i].left = -1;
  }

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

    for (j = 0; j < k; j++) {
      scanf("%d", &c);
      if (j == 0)
        a[id].left = c;
      else
        a[r].right = c;
      a[c].p = id;
      r = c;
    }
  }

  for (i = 0; i < n; i++) {
    depth = 0;
    P = a[i].p;

    while (P != -1) {
      P = a[P].p;
      depth++;
    }

    if (a[i].p == -1) {
      printf("node %d: parent = %d, depth = %d, root, [", i, a[i].p, depth);
    } else if (a[i].left == -1)
      printf("node %d: parent = %d, depth = %d, leaf, [", i, a[i].p, depth);
    else
      printf("node %d: parent = %d, depth = %d, internal node, [", i, a[i].p,
             depth);

    P = a[i].left;
    if (P != -1)
      printf("%d", P);

    while (P != -1) {
      P = a[P].right;
      if (P != -1)
        printf(", %d", P);
    }
    printf("]\n");
  }

  return 0;
}
Problem p02279 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define MAX 10000
#define NIL -1

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

Node T[MAX];

int depth(int i) {
  int count = 0;
  int c = i;
  while (T[c].parent != NIL) {
    c = T[c].parent;
    count++;
  }
  return count;
}

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

void printNodeChild(int i) {
  int c = T[i].left;

  while (c != NIL) {
    cout << c;
    c = T[c].right;

    if (c != NIL) {
      cout << ", ";
    }
  }
}

void printNodeInfo(int i) {
  cout << "node " << i << ": ";
  cout << "parent = " << T[i].parent << ",";
  cout << " depth = " << depth(i) << ",";
  cout << " " << nodeType(i) << ",";
  cout << " [";
  printNodeChild(i);
  cout << "]";

  cout << endl;
}

int main() {
  int n;

  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++) {
    int id, k, l;

    cin >> id >> k;

    for (int j = 0; j < k; j++) {
      int c;
      cin >> c;
      if (j == 0) {
        T[id].left = c;
      } else {
        T[l].right = c;
      }
      l = c;
      T[c].parent = id;
    }
  }

  int root = 0;

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

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

  // cout << root << endl;

  return 0;
}


Fixed submission:
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define MAX 100000
#define NIL -1

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

Node T[MAX];

int depth(int i) {
  int count = 0;
  int c = i;
  while (T[c].parent != NIL) {
    c = T[c].parent;
    count++;
  }
  return count;
}

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

void printNodeChild(int i) {
  int c = T[i].left;

  while (c != NIL) {
    cout << c;
    c = T[c].right;

    if (c != NIL) {
      cout << ", ";
    }
  }
}

void printNodeInfo(int i) {
  cout << "node " << i << ": ";
  cout << "parent = " << T[i].parent << ",";
  cout << " depth = " << depth(i) << ",";
  cout << " " << nodeType(i) << ",";
  cout << " [";
  printNodeChild(i);
  cout << "]";

  cout << endl;
}

int main() {
  int n;

  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++) {
    int id, k, l;

    cin >> id >> k;

    for (int j = 0; j < k; j++) {
      int c;
      cin >> c;
      if (j == 0) {
        T[id].left = c;
      } else {
        T[l].right = c;
      }
      l = c;
      T[c].parent = id;
    }
  }

  int root = 0;

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

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

  // cout << root << endl;

  return 0;
}
Problem p02279 (C++)

Buggy submission (status: 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 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;
}

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 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;
}
Problem p02279 (C++)

Buggy submission (status: Runtime Error):
#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");
  }
}

Fixed submission:
#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");
  }
}