Problem p02902 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define rep(i, n) REP(i, 0, n)
#define rep1(i, n) REP(i, 1, n)
#define repe(i, n) for (auto &&i : n)
#define all(v) (v).begin(), (v).end()
#define decimal fixed << setprecision(20)
#define fastcin()                                                              \
  cin.tie(0);                                                                  \
  ios::sync_with_stdio(false)
using namespace std;
using LL = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1e9;
const LL LLINF = 1e16;

using Graph = vector<set<int>>;

set<int> bfs(int v, Graph &G) {
  int n = G.size();
  vector<int> f(n, 0);
  f[v] = 1;
  queue<int> que;
  vector<int> prev(n, -1);
  que.push(v);
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    f[u] = 1;
    bool cyc = 0;
    repe(i, G[u]) {
      if (i == v) {
        cyc = 1;
        break;
      }
    }
    if (cyc) {
      set<int> ans;
      for (int j = u; j != v;) {
        ans.insert(j);
        j = prev[j];
      }
      ans.insert(v);
      return ans;
    }
    repe(i, G[u]) {
      que.push(i);
      prev[i] = u;
    }
  }
  return set<int>();
}

int main() {
  fastcin();

  int n, m;
  cin >> n >> m;
  Graph G(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].insert(b);
  }
  set<int> ans;
  int sz = INF;
  rep(i, n) {
    auto tmp = bfs(i, G);
    if (!tmp.empty() && tmp.size() < sz) {
      sz = tmp.size();
      ans = tmp;
    }
  }
  if (sz == INF)
    cout << -1 << endl;
  else {
    cout << sz << endl;
    repe(i, ans) cout << i + 1 << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define rep(i, n) REP(i, 0, n)
#define rep1(i, n) REP(i, 1, n)
#define repe(i, n) for (auto &&i : n)
#define all(v) (v).begin(), (v).end()
#define decimal fixed << setprecision(20)
#define fastcin()                                                              \
  cin.tie(0);                                                                  \
  ios::sync_with_stdio(false)
using namespace std;
using LL = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1e9;
const LL LLINF = 1e16;

using Graph = vector<set<int>>;

set<int> bfs(int v, Graph &G) {
  int n = G.size();
  vector<int> f(n, 0);
  f[v] = 1;
  queue<int> que;
  vector<int> prev(n, -1);
  que.push(v);
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    f[u] = 1;
    bool cyc = 0;
    repe(i, G[u]) {
      if (i == v) {
        cyc = 1;
        break;
      }
    }
    if (cyc) {
      set<int> ans;
      for (int j = u; j != v;) {
        ans.insert(j);
        j = prev[j];
      }
      ans.insert(v);
      return ans;
    }
    repe(i, G[u]) if (!f[i]) {
      que.push(i);
      prev[i] = u;
    }
  }
  return set<int>();
}

int main() {
  fastcin();

  int n, m;
  cin >> n >> m;
  Graph G(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].insert(b);
  }
  set<int> ans;
  int sz = INF;
  rep(i, n) {
    auto tmp = bfs(i, G);
    if (!tmp.empty() && tmp.size() < sz) {
      sz = tmp.size();
      ans = tmp;
    }
  }
  if (sz == INF)
    cout << -1 << endl;
  else {
    cout << sz << endl;
    repe(i, ans) cout << i + 1 << endl;
  }
}