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

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

int main() {
  while (1) {

    int n, m;
    cin >> n >> m;
    if (n == 0) {
      return 0;
    }
    vector<int> a(m);
    REP(i, n) {
      int d, v;
      cin >> d >> v;

      d--;
      if (v > a.at(d)) {
        a.at(d) = v;
      }
    }
    int sum = 0;
    REP(i, m) { sum += a.at(i); }
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  while (1) {

    int n, m;
    cin >> n >> m;
    vector<int> a(m);
    REP(i, n) {
      int d, v;
      cin >> d >> v;
      if (d == 0) {
        return 0;
      }
      d--;
      if (v > a.at(d)) {
        a.at(d) = v;
      }
    }
    int sum = 0;
    REP(i, m) { sum += a.at(i); }
    cout << sum << endl;
  }
}
Problem p01924 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  int t, d, l, x;

  int a[40];
  int count = 0;

  while (cin >> t >> d >> l) {
    int check = 0;
    int num = 0;

    if (t == 0 && d == 0 && l == 0) {
      break;
    }

    for (int i = 0; i < t; i++) {
      cin >> x;
      if (i != t - 1) {
        if (x >= l) {
          check = d;
        }
        if (check > 0) {
          num++;
          check--;
        }
      }
    }

    a[count] = num;
    count++;
  }

  for (int j = 0; j < count; j++) {
    cout << a[j] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int t, d, l;
  int set[40] = {0};
  int t_set[10000];
  int set_count = 0;
  int t_num;

  while (cin >> t >> d >> l) {
    if (t == 0 && d == 0 && l == 0) {
      break;
    }

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

    for (int j = 0; j < t; j++) {
      if (t_set[j] >= l) {
        if (t_num == 0) {
          if (j + d < t)
            set[set_count] += d;
          else
            set[set_count] += t - j - 1;
          t_num = d - 1;
        } else {
          set[set_count] -= t_num;
          set[set_count] += d;
          t_num = d - 1;
        }
      } else if (t_num != 0) {
        t_num--;
      }
    }

    set_count++;
  }

  for (int t = 0; t < set_count; t++) {
    cout << set[t] << endl;
  }

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

#include <iostream>
using namespace std;
int main() {
  while (1) {
    long int T, D, L, num = 0, flag = 0, i;
    cin >> T >> D >> L;
    if (T == 0 && D == 0 && L == 0)
      break;
    for (int y = 0; y < T; y++) {
      cin >> i;
      if (y != T - 1) {
        if (i >= L) {
          flag = D;
        }
        if (flag > 0) {
          num++;
          flag--;
        }
      }
    }
    cout << num << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  while (1) {
    long int T, D, L, i[1000000] = {0}, wet[1000000] = {0}, num = 0;
    cin >> T >> D >> L;
    if (T == 0 && D == 0 && L == 0)
      break;
    for (int y = 0; y < T; y++) {
      cin >> i[y];
      if (i[y] >= L) {
        for (int x = y; x < D + y; x++) {
          wet[x] = 1;
        }
      }
    }
    for (int y = 0; y < T - 1; y++) {
      num = num + wet[y];
    }
    cout << num << "\\n";
  }
  return 0;
}
Problem p01925 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(obj) obj.begin(), obj.end()
#define fcout cout << setprecision(10)

const int iINF = 1e9;
const long long llINF = 1e18;
const int MOD = 1e9 + 7;

using namespace std;

template <typename T> void debug(vector<T> &vec) {
  for (auto x : vec) {
    cout << x << " ";
  }
  cout << endl;
  return;
}

template <typename A, size_t N, typename T>
void FILL(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

template <typename T> int getFormer(const vector<T> &vec, T x) {
  return upper_bound(vec.begin(), vec.end(), x) - vec.begin() - 1;
}

template <typename T> int getLatter(const vector<T> &vec, T x) {
  return lower_bound(vec.begin(), vec.end(), x) - vec.begin();
}

void solve(int N, int M) {
  vector<pair<long long, int>> maxs(N, make_pair(0, 0)),
      mins(N, make_pair(0, 0));
  REP(i, N) {
    maxs[i].second = i;
    mins[i].second = i;
  }

  REP(i, M) {
    int S, k;
    cin >> S >> k;
    REP(j, k) {
      int c;
      cin >> c;
      maxs[c - 1].first += S;
      if (k == 1)
        mins[c - 1].first += S;
    }
  }

  sort(ALL(mins));
  sort(ALL(maxs), greater<pair<long long, int>>());

  if (maxs[0].second == mins[0].second) {
    cout << max(maxs[0].first - mins[1].first, maxs[1].first - mins[0].first) +
                1
         << endl;
  } else {
    cout << maxs[0].first - mins[0].first + 1 << endl;
  }

  return;
}

int main() {
  int N, M;
  while (cin >> N >> M && N) {
    solve(N, M);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(obj) obj.begin(), obj.end()
#define fcout cout << setprecision(10)

const int iINF = 1e9;
const long long llINF = 1e18;
const int MOD = 1e9 + 7;

using namespace std;

template <typename T> void debug(vector<T> &vec) {
  for (auto x : vec) {
    cout << x << " ";
  }
  cout << endl;
  return;
}

template <typename A, size_t N, typename T>
void FILL(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

template <typename T> int getFormer(const vector<T> &vec, T x) {
  return upper_bound(vec.begin(), vec.end(), x) - vec.begin() - 1;
}

template <typename T> int getLatter(const vector<T> &vec, T x) {
  return lower_bound(vec.begin(), vec.end(), x) - vec.begin();
}

void solve(int N, int M) {
  vector<pair<long long, int>> maxs(N, make_pair(0, 0)),
      mins(N, make_pair(0, 0));
  REP(i, N) {
    maxs[i].second = i;
    mins[i].second = i;
  }

  REP(i, M) {
    int S, k;
    REP(j, k) {
      int c;
      cin >> c;
      maxs[c - 1].first += S;
      if (k == 1)
        mins[c - 1].first += S;
    }
  }

  sort(ALL(maxs));
  sort(ALL(mins), greater<pair<long long, int>>());

  if (maxs[0].second == mins[0].second) {
    cout << max(maxs[0].first - mins[1].first, maxs[1].first - mins[0].first) +
                1
         << endl;
  } else {
    cout << maxs[0].first - mins[0].first + 1 << endl;
  }

  return;
}

int main() {
  int N, M;
  while (cin >> N >> M && N) {
    solve(N, M);
  }
  return 0;
}
Problem p01925 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;

    vector<pair<long long, long long>> v(n);
    rep(i, 0, m) {
      int s, k;
      cin >> s >> k;
      rep(j, 0, k) {
        int p;
        cin >> p;
        p--;
        v[p].first += s;
        if (k == 1)
          v[p].second += s;
      }
    }

    /*
    rep(i,0,n){
        cout << i << ":" << v[i].first << " " << v[i].second << endl;
    }
    */

    long long M = 0, mi = 0x7FFFFFFF;
    map<int, bool> mp = {};
    rep(i, 0, n) {
      if (M < v[i].first) {
        mp = map<int, bool>();
        mp[i] = true;
        M = v[i].first;
      } else if (M == v[i].first) {
        mp[i] = true;
      }
    }

    rep(i, 0, n) {
      if (v[i].second < mi && !mp[i]) {
        mi = v[i].second;
      }
    }
    cout << M - mi + 1 << endl;
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  ll N, M;
  while (cin >> N >> M, N) {
    auto S = vector<ll>(M);
    auto k = vector<ll>(M);
    auto c = vector<vector<ll>>(M, vector<ll>(M));
    auto sum = vector<ll>(N, 0);
    auto q = vector<vector<ll>>(N);
    for (int i = 0; i < M; ++i) {
      cin >> S[i] >> k[i];
      for (int j = 0; j < k[i]; ++j) {
        cin >> c[i][j];
        --c[i][j];
        sum[c[i][j]] += S[i];
        q[c[i][j]].push_back(i);
      }
    }
    auto maxIt = max_element(begin(sum), end(sum));
    int idx = distance(begin(sum), maxIt);
    for (auto &&x : q[idx])
      S[x] = 0;

    sum = vector<ll>(N, 0);
    for (int i = 0; i < M; ++i) {
      for (int j = 0; j < k[i]; ++j) {
        sum[c[i][j]] += S[i];
      }
    }
    sum[idx] = 1e9;
    auto minIt = min_element(begin(sum), end(sum));
    cout << *maxIt - *minIt + 1 << endl;
  }
  return 0;
}
Problem p01925 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N, M;
  while (cin >> N >> M, N || M) {
    vector<int> v(N), m(N);
    for (int i = 0; i < M; i++) {
      int s, k;
      cin >> s >> k;
      if (k == 1) {
        int t;
        cin >> t;
        v[t - 1] += s;
        m[t - 1] += s;
      } else {
        for (int j = 0; j < k; j++) {
          int t;
          cin >> t;
          v[t - 1] += s;
        }
      }
    }

    int first = -1, second = -1, idx, idy;
    for (int i = 0; i < N; i++) {
      if (first < v[i]) {
        second = first;
        idy = idx;
        first = v[i];
        idx = i;
      } else if (second < v[i]) {
        second = v[i];
        idy = i;
      }
    }

    int ans = 0;
    for (int i = 0; i < N; i++) {
      if (idx == i) {
        ans = max(ans, second - m[i]);
      } else {
        ans = max(ans, first - m[i]);
      }
    }

    cout << ans + 1 << endl;
  }

  return 0;
}

Error: -6
double free or corruption (out)


Fixed submission:

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

int main() {
  int N, M;
  while (cin >> N >> M, N || M) {
    vector<int> v(N);
    vector<int> min_cost(N);
    for (int i = 0; i < N; i++) {
      int s, k;
      cin >> s >> k;
      if (k == 1) {
        int t;
        cin >> t;
        v[t - 1] += s;
        min_cost[t - 1] += s;
        continue;
      }
      for (int j = 0; j < k; j++) {
        int t;
        cin >> t;
        v[t - 1] += s;
      }
    }

    int max_sum = 0;
    for (int i = 0; i < N; i++) {
      max_sum = max(max_sum, v[i]);
    }

    int ans = 0;
    for (int i = 0; i < N; i++) {
      ans = max(ans, max_sum - min_cost[i]);
    }

    cout << ans + 1 << endl;
  }

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

#include <algorithm>
#include <cstdio>
#include <vector>

int testcase_ends() {
  size_t N, M;
  scanf("%zu %zu", &N, &M);
  if (N == 0 && M == 0)
    return 1;

  std::vector<int> min(N), max(N);
  for (size_t i = 0; i < M; ++i) {
    int S;
    size_t k;
    scanf("%d %zu", &S, &k);
    std::vector<size_t> c(k);
    for (size_t j = 0; j < k; ++j) {
      scanf("%zu", &c[j]);
      --c[j];
    }

    if (k == 1) {
      min[c[0]] += S;
      max[c[0]] += S;
      continue;
    }
    for (size_t j = 0; j < k; ++j) {
      max[c[j]] += S;
    }
  }

  std::vector<int> max_left(N + 1), max_right(N + 1);

  for (size_t i = 0; i < N; ++i) {
    max_left[i + 1] = std::max(max_left[i], max[i]);
  }
  for (size_t i = N; i--;) {
    max_right[i] = std::max(max_right[i + 1], max[i]);
  }

  int res = 0;
  for (size_t i = 0; i < N; ++i) {
    int max0 = std::max(max_left[i], max_right[i + 1]);
    res = std::max(res, max0 - min[i] + 1);
  }
  printf("%d\\n", res);
  return 0;
}

int main() {
  while (!testcase_ends()) {
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

int testcase_ends() {
  size_t N, M;
  scanf("%zu %zu", &N, &M);
  if (N == 0 && M == 0)
    return 1;

  std::vector<int> min(N), max(N);
  for (size_t i = 0; i < M; ++i) {
    int S;
    size_t k;
    scanf("%d %zu", &S, &k);
    std::vector<size_t> c(k);
    for (size_t j = 0; j < k; ++j) {
      scanf("%zu", &c[j]);
      --c[j];
    }

    if (k == 1) {
      min[c[0]] += S;
      max[c[0]] += S;
      continue;
    }
    for (size_t j = 0; j < k; ++j) {
      max[c[j]] += S;
    }
  }

  std::vector<int> max_left(N), max_right(N + 1);

  for (size_t i = 0; i < N; ++i) {
    max_left[i + 1] = std::max(max_left[i], max[i]);
  }
  for (size_t i = N; i--;) {
    max_right[i] = std::max(max_right[i + 1], max[i]);
  }

  int res = 0;
  for (size_t i = 0; i < N; ++i) {
    int max0 = std::max(max_left[i], max_right[i + 1]);
    res = std::max(res, max0 - min[i] + 1);
  }
  printf("%d\\n", res);
  return 0;
}

int main() {
  while (!testcase_ends()) {
  }
}
Problem p01926 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int n, m;
int s[1000020];

int Check(int x) {
  int count = 0;
  int level = 1;
  int index = 0;
  while (true) {
    if (level + x > s[n - 1]) {
      count++;
      break;
    }
    int maxexp = x - abs(level - s[index]);
    while (true) {
      if (level + x <= s[index + 1])
        break;
      else {
        if (maxexp < x - abs(level - s[index + 1])) {
          index++;
          maxexp = x - abs(level - s[index]);
        } else {
          break;
        }
      }
    }
    count++;
    level += max(1, maxexp);
  }

  return count;
}

signed main() {
  while (true) {
    cin >> n >> m;
    if (n + m == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> s[i];
    }

    int l = s[0], r = s[n - 1] + 1;
    while (abs(r - l) > 1) {
      int x = (l + r) / 2;
      int check = Check(x);
      if (check >= m) {
        l = x;
      } else {
        r = x;
      }
    }

    if (Check(l) >= m)
      cout << l << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int n, m;
int s[1000020];

int Check(int x) {
  int count = 0;
  int level = 1;
  while (true) {
    if (level + x > s[n - 1]) {
      count++;
      break;
    }
    int index = lower_bound(s, s + n, x + level) - s;

    int l = 0, r = index;
    for (; r - l > 1;) {
      int mid = (r + l) / 2;
      if (x - abs(level - s[mid]) - (x - abs(level - s[mid - 1])) > 0) {
        l = mid;
      } else {
        r = mid;
      }
    }
    count++;
    level += max(1, x - abs(level - s[l]));
  }

  return count;
}

signed main() {
  while (true) {
    cin >> n >> m;
    if (n + m == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> s[i];
    }

    int l = s[0], r = s[n - 1] + 1;
    while (abs(r - l) > 1) {
      int x = (l + r) / 2;
      int check = Check(x);
      if (check >= m) {
        l = x;
      } else {
        r = x;
      }
    }

    if (Check(l) >= m) {
      cout << l << endl;
    } else
      cout << -1 << endl;
  }
  return 0;
}
Problem p01933 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;
#define repeat(i, n) for (int i = 0; i < (n); i++)

vector<int> g[100005]; // 0-indexed
int root[100005];
const int inf = 1e8;
vector<int> d(100005, inf);
int N, K;

void f(int i, int p = -1) {
  int ret = inf;
  for (int j : g[i])
    if (j != p) {
      f(j, i);
      ret = min(ret, d[j]);
    }
  if (ret == inf)
    d[i] = 0;
  else
    d[i] = ret + 1;
}

int main() {

  cin >> N >> K;
  repeat(i, N) {
    int x;
    cin >> x;
    if (x == 0) {
      root[i] = 1;
    } else {
      g[x - 1].push_back(i);
      // g[i].push_back(x-1);
    }
  }

  repeat(i, N) if (root[i]) { f(i); }

  int c = 0;
  repeat(i, N) {
    if (root[i])
      c++;
    else
      c += (d[i] < K);
  }
  cout << c << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;
#define repeat(i, n) for (int i = 0; i < (n); i++)

vector<int> g[100005]; // 0-indexed
int root[100005];
const int inf = 1e8;
vector<int> d(100005, inf);
int N, K;

void f(int i, int p = -1) {
  int ret = inf;
  for (int j : g[i])
    if (j != p) {
      f(j, i);
      ret = min(ret, d[j]);
    }
  if (ret == inf)
    d[i] = 0;
  else
    d[i] = ret + 1;
}

int main() {

  cin >> N >> K;
  repeat(i, N) {
    int x;
    cin >> x;
    if (x == 0) {
      root[i] = 1;
    } else {
      g[x - 1].push_back(i);
      g[i].push_back(x - 1);
    }
  }

  repeat(i, N) if (root[i]) { f(i); }

  int c = 0;
  repeat(i, N) {
    if (root[i])
      c++;
    else
      c += (d[i] < K);
  }
  cout << c << endl;

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

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e12;

int b[100010];
int f[100010];
vector<int> G[100010];
int n, k;
set<int> st;

int dfs(int now) {
  if (G[now].size() == 0) {
    st.insert(now);
    return 1;
  }
  int res = INF;
  rep(i, 0, G[now].size()) { chmin(res, dfs(G[now][i]) + 1); }
  if (res <= k)
    st.insert(now);
  return res;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  vector<int> a(n);
  rep(i, 0, n) {
    cin >> a[i];
    a[i]--;
    if (a[i] != -1) {
      b[a[i]]++;
      G[a[i]].push_back(i);
    }
  }
  // set<int> st;
  rep(i, 0, n) {
    if (a[i] == -1) {
      st.insert(i);
      dfs(i);
    }
  }
  // cout << ans << endl;
  cout << st.size() << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e12;

int b[100010];
int f[100010];

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, 0, n) {
    cin >> a[i];
    a[i]--;
    if (a[i] != -1) {
      b[a[i]]++;
    }
  }
  set<int> st;
  rep(i, 0, n) {
    if (a[i] == -1) {
      st.insert(i);
      f[i] = 1;
    } else if (b[i] == 0) {
      int now = i;
      rep(j, 0, k) {
        if (now == -1)
          break;
        if (f[now] > k - j)
          break;
        // cout << now << endl;
        st.insert(now);
        f[now] = k - j;
        now = a[now];
      }
    }
  }
  cout << st.size() << endl;
}
Problem p01933 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, n) for (int i = 0; i < (n); i++)
#define MEM(a, x) memset(a, x, sizeof(a))
#define ALL(a) a.begin(), a.end()
#define UNIQUE(a) a.erase(unique(ALL(a)), a.end())
typedef long long ll;

int n, k, d[100005], INF = 1e9;
vector<int> g[100005];
bool a[100005], b[100005], c[100005];

void dfs(int x, int cnt) {
  c[x] = true;
  d[x] = min(d[x], cnt);
  if (cnt + 1 < k) {
    FOR(i, g[x].size()) {
      if (d[g[x][i]] > cnt + 1)
        dfs(g[x][i], cnt + 1);
    }
  }
  return;
}

int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);
  cin >> n >> k;
  FOR(i, n) {
    int x;
    cin >> x;
    if (x == 0)
      a[i] = true;
    else {
      b[x - 1] = true;
      g[i].push_back(x - 1);
    }
  }
  fill(d, d + n, INF);
  FOR(i, n) {
    if (!b[i] && !a[i])
      dfs(i, 0);
  }
  int ret = 0;
  FOR(i, n) {
    if (a[i] || c[i])
      ret++;
  }
  cout << ret << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, n) for (int i = 0; i < (n); i++)
#define MEM(a, x) memset(a, x, sizeof(a))
#define ALL(a) a.begin(), a.end()
#define UNIQUE(a) a.erase(unique(ALL(a)), a.end())
typedef long long ll;

int n, k;
vector<int> g[100005];
bool a[100005], b[100005], c[100005];

void dfs(int x, int cnt) {
  c[x] = true;
  if (cnt + 1 < k) {
    FOR(i, g[x].size()) dfs(g[x][i], cnt + 1);
  }
  return;
}

int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);
  cin >> n >> k;
  FOR(i, n) {
    int x;
    cin >> x;
    if (x == 0)
      a[i] = true;
    else {
      b[x - 1] = true;
      g[i].push_back(x - 1);
    }
  }
  FOR(i, n) {
    if (!b[i])
      dfs(i, 0);
  }
  int ret = 0;
  FOR(i, n) {
    if (a[i] || c[i])
      ret++;
  }
  cout << ret << endl;
  return 0;
}
Problem p01933 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, a, n) for (int i = ((int)a); i < ((int)n); i++)
#define INF (1 << 30)
using namespace std;

int N, K, A;
vector<int> R, E[100000];
bool ans[100000];

int dfs(int v) {
  int depth = INF;
  REP(i, 0, E[v].size()) depth = min(depth, dfs(E[v][i]));
  if (depth == INF)
    depth = 0;
  if (depth < K)
    ans[v] = true;
  return depth + 1;
}

int main(void) {
  cin >> N >> K;
  REP(i, 0, N) {
    cin >> A;
    if (A >= 1)
      E[A - 1].push_back(i);
    else
      R.push_back(i);
  }

  REP(i, 0, N) ans[i] = false;
  REP(i, 0, R.size()) ans[R[i]] = true, dfs(R[i]);

  int cnt = 0;
  REP(i, 0, N) if (ans[i]) cnt++;
  cout << cnt << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, a, n) for (int i = ((int)a); i < ((int)n); i++)
#define INF (1 << 30)
using namespace std;

int N, K, A;
vector<int> R, E[100000];
bool ans[100000];

int dfs(int v) {
  int depth = INF;
  for (int u : E[v])
    depth = min(depth, dfs(u));
  if (depth == INF)
    depth = 0;
  if (depth < K)
    ans[v] = true;
  return depth + 1;
}

int main(void) {
  cin >> N >> K;
  REP(i, 0, N) {
    cin >> A;
    if (A >= 1)
      E[A - 1].push_back(i);
    else
      R.push_back(i);
  }

  REP(i, 0, N) ans[i] = false;
  REP(i, 0, R.size()) ans[R[i]] = true, dfs(R[i]);

  int cnt = 0;
  REP(i, 0, N) if (ans[i]) cnt++;
  cout << cnt << endl;
}
Problem p01933 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;
#define fi first
#define se second
int n, k, x;
vector<int> v[100002];
int d[100002], ans;
bool b[100002], c[100002];
bool t[100002], ng[100002];
int main() {
  r(i, 100002) d[i] = 1e8;
  cin >> n >> k;
  r(i, n) {
    scanf("%d", &x);
    if (x) {
      v[i].push_back(x - 1);
      b[i] = 1;
      t[x - 1] = 1;
    }
  }
  queue<P> q;
  r(i, n) {
    if (!t[i]) {
      q.push(P(i, 0));
      c[i] = 1;
    }
  }
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    x = p.fi;
    int dis = p.se;
    if (dis >= k - 1)
      continue;
    r(i, v[x].size()) {
      if (!c[v[x][i]]) {
        c[v[x][i]] = 1;
        q.push(P(v[x][i], dis + 1));
      }
    }
  }
  r(i, n) {
    if (c[i] || !b[i]) {
      ans++;
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
int n, k, x;
vector<int> v[100002];
int d[100002], ans;
bool b[100002], c[100002];
bool t[100002], ng[100002];
void dfs2(int p) {
  if (ng[p])
    return;
  ng[p] = 1;
  r(i, v[p].size()) { dfs2(v[p][i]); }
}
void dfs(int p, int de) {
  c[p] = 1;
  r(i, v[p].size()) {
    if (de < k - 1) {
      dfs(v[p][i], de + 1);
    } else {
      dfs2(v[p][i]);
    }
  }
}
int main() {
  r(i, 100002) d[i] = 1e8;
  cin >> n >> k;
  r(i, n) {
    scanf("%d", &x);
    if (x) {
      v[i].push_back(x - 1);
      b[i] = 1;
      t[x - 1] = 1;
    }
  }
  r(i, n) {
    if (!t[i]) {
      dfs(i, 0);
    }
  }
  r(i, n) {
    if (c[i] || !b[i]) {
      ans++;
    }
  }
  cout << ans << endl;
}