Problem p02902 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#define int long long
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int(i) = ((n)-1); (i) >= 0; (i)--)
#define itn int
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
const long long INF = 1LL << 60;
const int MOD = 1000000007;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
signed main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  int cnt = 0;
  int ans = 1005;
  vector<int> as;
  vector<vector<int>> g(1005, vector<int>());
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    g[a - 1].push_back(b - 1);
  }
  bool v[1005];
  memset(v, 0, 1005);
  vector<int> mu;
  rep(i, n) {
    queue<pair<int, vector<int>>> q;
    q.push({i, mu});
    while (!q.empty()) {
      cnt++;
      if (cnt == 5000000) {
        cnt = 0;
        break;
      }
      auto p = q.front();
      q.pop();
      if (p.S.size() >= ans)
        continue;
      if (v[p.F]) {
        if (p.F == i) {
          if (ans > p.S.size()) {
            ans = p.S.size();
            as = p.S;
          }
        }
        continue;
      }
      v[i] = 1;
      p.S.push_back(p.F);
      for (int j = 0; j < g[p.F].size(); j++) {
        q.push({g[p.F][j], p.S});
      }
    }
    memset(v, 0, 1005);
  }

  if (ans != 1005) {
    cout << ans << '\n';
    rep(i, ans) cout << as[i] + 1 << ' ';
  } else
    cout << -1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#define int long long
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int(i) = ((n)-1); (i) >= 0; (i)--)
#define itn int
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
const long long INF = 1LL << 60;
const int MOD = 1000000007;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
signed main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  int cnt = 0;
  int ans = 1005;
  vector<int> as;
  vector<vector<int>> g(1005, vector<int>());
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    g[a - 1].push_back(b - 1);
  }
  bool v[1005];
  memset(v, 0, 1005);
  vector<int> mu;
  rep(i, n) {
    queue<pair<int, vector<int>>> q;
    q.push({i, mu});
    while (!q.empty()) {
      cnt++;
      if (cnt == 100000) {
        cnt = 0;
        break;
      }
      auto p = q.front();
      q.pop();
      if (p.S.size() >= ans)
        continue;
      if (v[p.F]) {
        if (p.F == i) {
          if (ans > p.S.size()) {
            ans = p.S.size();
            as = p.S;
          }
        }
        continue;
      }
      v[i] = 1;
      p.S.push_back(p.F);
      for (int j = 0; j < g[p.F].size(); j++) {
        q.push({g[p.F][j], p.S});
      }
    }
    memset(v, 0, 1005);
  }

  if (ans != 1005) {
    cout << ans << '\n';
    rep(i, ans) cout << as[i] + 1 << ' ';
  } else
    cout << -1 << endl;
}