Problem p00344 (C++) — buggy submission, verdict: Time Limit Exceeded

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

// #define int long long
#define DBG 0
#define dump(o)                                                                \\
  if (DBG) {                                                                   \\
    cerr << #o << " " << o << endl;                                            \\
  }
#define dumpc(o)                                                               \\
  if (DBG) {                                                                   \\
    cerr << #o;                                                                \\
    for (auto &e : (o))                                                        \\
      cerr << " " << e;                                                        \\
    cerr << endl;                                                              \\
  }
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define each(it, c) for (auto it = (c).begin(); it != (c).end(); it++)
#define all(c) c.begin(), c.end()
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);

signed main() {
  int N;
  cin >> N;
  vector<int> in(N, 0); //??\\?¬???°
  vector<int> next(N);  //?¬?????????????id
  rep(i, 0, N) {
    int v;
    cin >> v;
    v = (i + v) % N;
    next[i] = v;
    in[v]++;
  }
  dumpc(in);
  dumpc(next);

  queue<int> Q;
  rep(i, 0, N) if (in[i] == 0) Q.push(
      i); //??\\?¬???°???0?????????(????????????????????????)????????\\????????\\??????

  int ans = N;
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    ans--;
    int v = next[u];
    in[v]--;
    if (in[v] == 0)
      Q.push(
          v); //??\\?¬???°???0?????????(????????????????????????)????????\\????????\\??????
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

// #define int long long
#define DBG 1
#define dump(o)                                                                \\
  if (DBG) {                                                                   \\
    cerr << #o << " " << o << endl;                                            \\
  }
#define dumpc(o)                                                               \\
  if (DBG) {                                                                   \\
    cerr << #o;                                                                \\
    for (auto &e : (o))                                                        \\
      cerr << " " << e;                                                        \\
    cerr << endl;                                                              \\
  }
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define each(it, c) for (auto it = (c).begin(); it != (c).end(); it++)
#define all(c) c.begin(), c.end()
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);

signed main() {
  int N;
  cin >> N;
  vector<int> a(N);
  rep(i, 0, N) { cin >> a[i]; }
  vector<char> g(N, false);
  int ans = 0;
  vector<char> f;
  rep(i, 0, a.size()) {
    f = vector<char>(N, false);
    int j = (i + a[i]) % N;
    if (g[i]) {
      ans++;
      continue;
    }
    while (true) {
      if (g[j])
        break;
      if (j == i) {
        ans++;
        rep(k, 0, N) g[k] |= f[k];
        break;
      } else {
        if (f[j])
          break;
        f[j] = true;
        j += a[j];
        j %= N;
      }
    }
  }
  cout << ans << endl;
  return 0;
}