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

#include <stdio.h>

#define NMAX 1000
#define WMAX 10000

int mincost(short *, int, int);

int main() {

  int i, j, n, d, wtemp, wmin = WMAX, cost = 0;
  short w[NMAX], s[NMAX], l[NMAX];
  char c[WMAX + 1] = {0}, flag[NMAX] = {0};

  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    scanf("%d", &w[i]);
    if (wmin > w[i])
      wmin = w[i];
    c[w[i]]++;
  }

  j = 0;

  for (i = 0; i <= WMAX; i++) {
    if (c[i]) {
      s[j] = i;
      j++;
    }
  }

  for (i = 0; i < n; i++) {
    if (flag[i] == 0) {
      d = 0;
      flag[i]++;
      l[d] = w[i];
      d++;
      wtemp = w[i];
      while (1) {
        j = 0;
        while (s[j] != wtemp)
          j++;
        if (j == i)
          break;
        flag[j]++;
        l[d] = w[j];
        d++;
        wtemp = w[j];
      }
      cost += mincost(l, d, wmin);
    }
  }

  printf("%d\\n", cost);

  return 0;
}

int mincost(short *l, int d, int wmin) {
  int i, c1 = 0, c2, lmin = WMAX, imin;

  for (i = 0; i < d; i++) {
    c1 += l[i];
    if (lmin > l[i]) {
      lmin = l[i];
    }
  }

  c2 = c1;
  c1 += (d - 2) * lmin;
  c2 += lmin + (d + 1) * wmin;

  if (c1 < c2)
    return c1;
  else
    return c2;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#define N 10001

typedef struct {
  int sum;
  int flag;
} CC;

CC C[N];
int A[1000], B[1000];

int main() {

  int i, n, min = 0, m, k, now, sum = 0, minx, sum1, sum2;

  scanf("%d", &n);

  for (i = 1; i < n + 1; i++) {
    scanf("%d", &A[i]);
  }

  minx = A[1];

  for (i = 1; i < n + 1; i++)
    if (minx > A[i])
      minx = A[i];

  for (i = 0; i < N; i++) {
    C[i].sum = 0;
    C[i].flag = 0;
  }

  for (i = 1; i < n + 1; i++) {
    C[A[i]].sum += 1;
    C[A[i]].flag += 1;
  }

  for (i = 0; i < N; i++)
    C[i].sum += C[i - 1].sum;

  for (i = n + 1; i > 0; i--) {
    if (C[A[i]].flag == 1) {
      k = i;
      now = k;
      m = 0;
      B[C[A[k]].sum] = A[k];
      C[A[i]].flag = 0;
      min = A[k];
      k = C[A[k]].sum;
      while (A[now] != A[k]) {
        if (m == 0)
          sum += A[now];
        B[C[A[k]].sum] = A[k];
        C[A[k]].flag = 0;
        sum += A[k];
        if (min > A[k])
          min = A[k];
        k = C[A[k]].sum;
        m += 1;
      }
      sum2 = min * (m - 1);
      sum1 = min + (m + 2) * minx;
      if (m > 1) {
        if (sum1 < sum2) {
          sum += sum1;
        } else
          sum += sum2;
        m = 0;
        min = 0;
      }
    }
  }

  printf("%d\\n", sum);

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

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

Error: 0

Fixed submission:

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

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
int main(void) {
  std::ios_base::sync_with_stdio(false);
  int n, input;
  std::cin >> n;
  std::map<int, int> w;
  std::vector<int> vector(n + 1, 0);
  for (int i = 1; i <= n; ++i) {
    std::cin >> input;
    vector[i] = input;
  }
  std::vector<int> vector_min = vector;
  std::sort(vector_min.begin(), vector_min.end());
  for (int i = 1; i <= n; ++i) {
    w[vector[i]] = vector_min[i];
  }
  std::map<int, int> w_1(w);
  int cnt = 0;
  for (int i = 1; i <= n; ++i) {
    int temp = 0;
    while (vector_min[i] != w[vector_min[i]]) {
      temp += vector_min[i] + w[vector_min[i]];
      std::swap(w[vector_min[i]], w[w[vector_min[i]]]);
    }
    int temp_1 = static_cast<int>(1e5);
    if (i > 1 && vector_min[i] != w_1[vector_min[i]]) {
      temp_1 = vector_min[i] + vector_min[1];

      std::swap(w_1[vector_min[i]], w_1[vector_min[1]]);
      while (vector_min[1] != w_1[vector_min[1]]) {
        temp_1 += vector_min[1] + w_1[vector_min[1]];
        std::swap(w_1[vector_min[1]], w_1[w_1[vector_min[1]]]);
      }
    } else if (i == 1) {
      while (vector_min[1] != w_1[vector_min[1]]) {
        temp_1 += vector_min[i] + w_1[vector_min[i]];
        std::swap(w_1[vector_min[i]], w_1[w_1[vector_min[i]]]);
      }
    }
    cnt += std::min(temp, temp_1);
  }
  std::cout << cnt << std::endl;
  // system("pause");
  return 0;
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
int main(void) {
  std::ios_base::sync_with_stdio(false);
  int n, input;
  std::cin >> n;
  std::map<int, int> w;
  std::vector<int> vector(n);

  for (int i = 0; i < n; ++i) {
    std::cin >> input;
    w[input - 1] = i;
    vector[i] = input - 1;
  }
  std::vector<int> vector_min = vector;
  std::map<int, int> w_1(w);
  std::sort(vector_min.begin(), vector_min.end());
  for (int i = 0; i < n; ++i) {
    w[vector[i]] = vector_min[i];
  }
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    int temp = 0;
    while (vector_min[i] != w[vector_min[i]]) {
      temp += vector_min[i] + w[vector_min[i]] + 2;
      std::swap(w[vector_min[i]], w[w[vector_min[i]]]);
    }
    int temp_1 = static_cast<int>(1e5);
    if (i > 0 && vector_min[i] != w_1[vector_min[i]]) {
      temp_1 = w_1[vector_min[i]] + w_1[vector_min[0]] + 2;
      std::swap(w_1[vector_min[0]], w_1[vector_min[i]]);
      while (vector_min[0] != w_1[vector_min[0]]) {
        temp_1 += w_1[vector_min[i]] + w_1[vector_min[0]] + 2;
        std::swap(w_1[vector_min[0]], w_1[w_1[vector_min[0]]]);
      }
    }
    cnt += std::min(temp, temp_1);
  }
  std::cout << cnt << std::endl;
  //	system("pause");
  return 0;
}
Problem p02278 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

Error: MLE

Fixed submission:

#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;
}
Problem p02278 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, ans = 0, amin = 10000;
  cin >> n;
  int a[n], b[n], T[10000];
  bool V[n];
  for (int i = 0; i < n; i++)
    V[i] = true;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i];
    amin = min(a[i], amin);
  }
  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 j = i, tmpmin = a[i], c = 0;
    int S = 0;
    while (V[j]) {
      c++;
      tmpmin = min(tmpmin, a[j]);
      S += a[j];
      V[j] = false;
      j = T[a[j]];
    }
    ans += min(S + (c - 2) * tmpmin, S + tmpmin + (c + 1) * amin);
  }

  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  int n, ans = 0, amin = 10000;
  cin >> n;
  int a[n], b[n], T[n];
  bool V[n];
  for (int i = 0; i < n; i++)
    V[i] = true;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i];
    amin = min(a[i], amin);
  }
  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 j = i, tmpmin = a[i], c = 0;
    while (V[j]) {
      c++;
      tmpmin = min(tmpmin, a[j]);
      ans += a[j];
      V[j] = false;
      j = T[a[j]];
    }
    ans += min((c - 2) * tmpmin, tmpmin + (c + 1) * amin);
  }

  cout << ans << endl;
  return 0;
}
Problem p02278 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#pragma warning(disable : 4996)
using namespace std;
int n, a[1009], c[1009];
bool vis[1009];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (a[i] > a[j])
        c[i]++;
    }
  }
  int z = *min_element(a, a + n);
  int ret = 0;
  for (int i = 0; i < n; i++) {
    if (vis[i])
      continue;
    int pos = i, m = 1012345678, cnt = 0;
    while (!vis[pos]) {
      vis[pos] = true;
      ret += a[pos];
      m = min(m, a[pos]);
      pos = c[pos];
      cnt++;
    }
    ret += min(m * (cnt - 2), z * (cnt + 1) + m);
  }
  printf("%d\\n", ret);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <map>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

int sorting(vector<short> v) {
  unsigned char size_ = v.size();

  map<vector<short>, int> d;

  vector<short> sorted(v.begin(), v.end());

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

  queue<pair<vector<short>, int>> que;

  que.push(make_pair(v, 1));
  d[v] = 1; // pair(vector, cost + 1)

  while (!que.empty()) {
    vector<short> v1 = que.front().first;

    int cost = que.front().second;
    que.pop();

    for (char i = 0; i < size_; i++) {
      for (char j = i + 1; j < size_; j++) {
        vector<short> v2(v1.begin(), v1.end());

        int cost2 = cost + v1[i] + v1[j];

        swap(v2[i], v2[j]);

        if (d[v2] == 0 || d[v2] > cost2) {
          d[v2] = cost2;

          que.push(make_pair(v2, cost2));
        }
      }
    }
  }

  return d[sorted] - 1;
}

int main() {
  vector<short> v;
  int n;

  scanf("%d", &n);
  v = vector<short>(n);

  for (int i = 0; i < n; i++) {
    scanf("%d", &v[i]);
  }

  printf("%d\\n", sorting(v));

  return 0;
}
Problem p02278 (C++) — buggy submission, verdict: 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[VMAX];
  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 an = 0;
    int m = VMAX;
    while (1) {
      V[cur] = true;
      an++;
      int v = A[cur];
      m = min(m, v);
      S += v;
      cur = T[v];
      if (V[cur])
        break;
    }
    ans += min(S + (an - 2) * m, S + m + (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;

  return 0;
}


Error: TLE

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[VMAX];
  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 an = 0;
    int m = VMAX;
    while (1) {
      V[cur] = false;
      an++;
      int v = A[cur];
      m = min(m, v);
      s += v;
      cur = T[v];
      if (V[cur])
        break;
    }
    ans += min(S + (an - 2) * m, S + m + (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;

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

#include <algorithm>
#include <cstdint>
#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

int main() {
  int32_t n;

  cin >> n;
  vector<int32_t> w(n);
  for (int32_t i = 0; i < n; i++) {
    cin >> w[i];
  }

  vector<int32_t> sorted(w);
  sort(sorted.begin(), sorted.end());

  unordered_map<int32_t, int32_t> index;
  for (int32_t i = 0; i < n; i++) {
    index[sorted[i]] = i;
  }

  int32_t cost = 0;
  for (int32_t i = 0; i < n; i++) {
    if (w[i] != sorted[i]) {
      int32_t size = 1, sum = w[i], minval = INT32_MAX;
      int32_t j = index[w[i]], j_new;
      while (j != i) {
        size++;
        sum += w[j];
        minval = min(minval, w[j]);
        j_new = index[w[j]];
        w[j] = sorted[j];
        j = j_new;
      }

      cost += min(sum + minval * (size - 2), 2 * (sorted[0] + minval) +
                                                 (sum - minval + sorted[0]) +
                                                 sorted[0] * (size - 2));
    }
  }

  cout << cost << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdint>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

template <typename T>
int32_t merge(vector<T> &A, int32_t left, int32_t mid, int32_t right) {
  int32_t n1 = mid - left;
  int32_t n2 = right - mid;

  vector<T> L(n1 + 1);
  vector<T> R(n2 + 1);
  for (int32_t i = 0; i < n1; i++) {
    L[i] = A[left + i];
  }
  for (int32_t i = 0; i < n2; i++) {
    R[i] = A[mid + i];
  }
  L[n1] = INT32_MAX;
  R[n2] = INT32_MAX;

  int32_t i = 0, j = 0, count = 0;
  for (int32_t k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i++;
    } else {
      A[k] = R[j];
      j++;
    }
    count++;
  }

  return count;
}

template <typename T>
int32_t cost(const vector<T> &unsorted, const vector<T> &sorted) {
  bool is_sorted = true;
  int32_t c = INT32_MAX;

  for (int32_t i = 0; i < unsorted.size(); i++) {
    if (unsorted[i] != sorted[i]) {
      is_sorted = false;

      int32_t j = 0;
      while (unsorted[i] != sorted[j])
        j++;

      vector<T> unsorted2(unsorted);
      swap(unsorted2[i], unsorted2[j]);
      c = min(c, unsorted2[i] + unsorted2[j] + cost(unsorted2, sorted));
    }
  }

  return (is_sorted ? 0 : c);
}

template <typename T>
int32_t merge_sort(vector<T> &A, int32_t left, int32_t right) {
  if (left + 1 < right) {
    int32_t mid = left + (right - left) / 2;
    int32_t count1 = merge_sort(A, left, mid);
    int32_t count2 = merge_sort(A, mid, right);
    int32_t count3 = merge(A, left, mid, right);
    return count1 + count2 + count3;
  }

  return 0;
}

int main() {
  int32_t n;

  cin >> n;
  vector<int32_t> w1(n);
  for (int32_t i = 0; i < n; i++) {
    cin >> w1[i];
  }

  vector<int32_t> w2(w1);
  merge_sort(w2, 0, n);

  cout << cost(w1, w2) << endl;

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

#include <cstring>
#include <iostream>
using namespace std;
struct Node {
  int sib;
  int par;
  int chi;
  int dep;
};

Node N[100000] = {-1};

int root;

void print(int id) {
  Node cur = N[id];
  cout << "node " << id << ": parent = " << cur.par;
  cout << ", depth = " << cur.dep;
  if (id == root)
    cout << ", root, [";
  else if (cur.chi == -1) {
    cout << ", leaf, []" << endl;
    return;
  } else
    cout << ", internal node, [";
  int chi = cur.chi;
  while (chi != -1) {
    cout << chi;
    chi = N[chi].sib;
    if (chi == -1)
      break;
    cout << ", ";
  }
  cout << ']' << endl;
}

int main() {
  memset(N, -1, sizeof(N));
  int n, id, c, chi, sib;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> id >> c;
    for (int j = 0; j < c; j++) {
      cin >> chi;
      if (j == 0)
        N[id].chi = chi;
      else
        N[sib].sib = chi;
      N[chi].par = id;
      sib = chi;
    }
  }

  for (int i = 0; i < 100000; i++) {
    if ((N[i].chi != -1) && (N[i].par == -1)) {
      root = i;
    }
  }

  id = root;
  int d = 0;
  while (1) {
    Node &cur = N[id];
    cur.dep = d;
    if (cur.chi != -1) {
      id = cur.chi;
      d++;
    } else if (cur.sib != -1)
      id = cur.sib;
    else {
      while (1) {
        if (d == 0)
          break;
        d--;
        id = N[id].par;
        if (N[id].sib != -1) {
          id = N[id].sib;
          break;
        }
      }
    }
    if (d == 0)
      break;
  }

  for (int i = 0; i < 100000; i++) {
    if (N[i].dep != -1) {
      print(i);
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;
struct Node {
  int sib;
  int par;
  int chi;
  int dep;
};

Node N[100000] = {-1};

int root;

void print(int id) {
  Node cur = N[id];
  cout << "node " << id << ": parent = " << cur.par;
  cout << ", depth = " << cur.dep;
  if (cur.chi == -1) {
    cout << ", leaf, []" << endl;
    return;
  } else if (id == root)
    cout << ", root, [";
  else
    cout << ", internal node, [";
  int chi = cur.chi;
  while (1) {
    cout << chi;
    chi = N[chi].sib;
    if (chi == -1)
      break;
    cout << ", ";
  }
  cout << ']' << endl;
}

int main() {
  memset(N, -1, sizeof(N));
  int n, id, c, chi, sib;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> id >> c;
    for (int j = 0; j < c; j++) {
      cin >> chi;
      if (j == 0)
        N[id].chi = chi;
      else
        N[sib].sib = chi;
      N[chi].par = id;
      sib = chi;
    }
  }

  for (int i = 0; i < 100000; i++) {
    if ((N[i].chi != -1) && (N[i].par == -1)) {
      root = i;
    }
  }

  id = root;
  int d = 0;
  while (1) {
    Node &cur = N[id];
    cur.dep = d;
    print(id);
    if (cur.chi != -1) {
      id = cur.chi;
      d++;
    } else if (cur.sib != -1)
      id = cur.sib;
    else {
      while (1) {
        id = N[id].par;
        d--;
        if (d == 0)
          return 0;
        if (N[id].sib != -1) {
          id = N[id].sib;
          break;
        }
      }
    }
  }
  return 0;
}
Problem p02279 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
using namespace std;
const int num = 100000;
int n;
const int NIL = -1;
struct Node {
  int p, r, l;
};
Node T[num];
int D[num];
inline void rec(int u, int p) {
  D[u] = p;
  if (T[u].l != NIL)
    rec(T[u].l, p + 1);
  if (T[u].r != NIL)
    rec(T[u].r, p);
}

void print(int u) {
  int i;
  printf("node %d: parent = %d, depth = %d, ", u, T[u].p, D[u]);
  if (T[u].p == NIL)
    printf("root, [");
  else if (T[u].l == NIL)
    printf("leaf, [");
  else
    printf("internal node, [");
  int c;
  for (i = 0, c = T[u].l; c != NIL; c = T[c].r, i++) {

    if (i)
      printf(", ");
    printf("%d", c);
  }

  printf("]\\n");
}
int main() {
  int numn, numc, v, parentnum, c, i, j, a;
  cin >> 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", &numn, &numc);
    for (j = 0; j < numc; j++) {
      scanf("%d", &c);
      if (j == 0)
        T[numn].l = c;
      else
        T[v].r = c;
      v = c;
      T[c].p = numn;
    }
  }
  for (i = 0; i < n; i++)
    if (T[i].p == NIL)
      parentnum = i;
  rec(parentnum, 0);
  // for (i = 0; i < n; i++) print(i);
  for (a = 0; a < n; a++) {
    printf("node %d: parent = %d, depth = %d, ", a, T[a].p, D[a]);
    if (T[a].p == NIL)
      printf("root, [");
    else if (T[a].l == NIL)
      printf("leaf, [");
    else
      printf("internal node, [");
    for (i = 0, c = T[a].l; c != NIL; c = T[c].r, i++) {
      if (i)
        printf(", ");
      printf("%d", c);
    }
    printf("]\\n");
  }
  return 0;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
using namespace std;
const int num = 100000;
int i, j;
const int NIL = -1;
struct Node {
  int p, r, l;
};
Node T[num];
int D[num];
inline void rec(int u, int p) {
  D[u] = p;
  if (T[u].l != NIL)
    rec(T[u].l, p + 1);
  if (T[u].r != NIL)
    rec(T[u].r, p);
}
int main() {
  int n, numn, numc, v, parentnum, c;
  cin >> 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", &numn, &numc);
    for (j = 0; j < numc; j++) {
      scanf("%d", &c);
      if (j == 0)
        T[numn].l = c;
      else
        T[v].r = c;
      v = c;
      T[v].p = numn;
    }
  }
  for (i = 0; i < n; i++)
    if (T[i].p = NIL)
      parentnum = i;
  rec(parentnum, 0);
  for (i = 0; i < n; i++) {
    printf("node %d: parent = %d, depth = %d, ", i, T[i].p, D[i]);
    if (T[i].p == NIL)
      printf("root, [");
    else if (T[i].l == NIL)
      printf("leaf, [");
    else
      printf("internal node, [");
    for (i = 0, c = T[i].l; c != NIL; c = T[c].r, i++) {
      if (i)
        printf(", ");
      printf("%d", c);
    }
    printf("]\\n");
  }
}