Problem p02902 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vt<T>>;
template <class T> using ttt = tuple<T, T>;
using tii = tuple<int, int>;
using tiii = tuple<int, int, int>;
using vi = vector<int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define mt make_tuple
#define ALL(a) (a).begin(), (a).end()
#define FST first
#define SEC second
#define DEB cerr << "!" << endl
#define SHOW(a, b) cerr << (a) << " " << (b) << endl
const int INF = (INT_MAX / 2);
const ll LLINF = (LLONG_MAX / 2);
const double eps = 1e-8;
const ll DIV = 1e9 + 7;
// const double PI = M_PI;
inline ll pow(ll x, ll n, ll m) {
  ll r = 1;
  while (n > 0) {
    if ((n & 1) == 1)
      r = r * x % m;
    x = x * x % m;
    n >>= 1;
  }
  return r % m;
}
inline ll lcm(ll d1, ll d2) { return d1 / __gcd(d1, d2) * d2; }
#define chmax(a, b) a = max(a, b)

/*Coding Space*/
vector<int> ans;
vector<int> G[1001];
int main() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    G[a].pb(b);
  }
  rep(i, n) {
    int s = i + 1;
    bool used[1001] = {};
    vt<int> ss;
    ss.pb(s);
    queue<vector<int>> q;
    q.push(ss);
    while (!q.empty()) {
      auto now = q.front();
      q.pop();
      if (now.size() >= 2 && *now.rbegin() == s) {
        if (ans.size() == 0 || ans.size() > now.size())
          ans = now;
        break;
      }
      auto n = *now.rbegin();
      for (auto nn : G[n]) {
        now.pb(nn);
        q.push(now);
        now.pop_back();
      }
    }
  }
  if (ans.size() == 0)
    cout << -1 << endl;
  else {
    cout << ans.size() - 1 << endl;
    rep(i, ans.size() - 1) cout << ans[i] << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vt<T>>;
template <class T> using ttt = tuple<T, T>;
using tii = tuple<int, int>;
using tiii = tuple<int, int, int>;
using vi = vector<int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define mt make_tuple
#define ALL(a) (a).begin(), (a).end()
#define FST first
#define SEC second
#define DEB cerr << "!" << endl
#define SHOW(a, b) cerr << (a) << " " << (b) << endl
const int INF = (INT_MAX / 2);
const ll LLINF = (LLONG_MAX / 2);
const double eps = 1e-8;
const ll DIV = 1e9 + 7;
// const double PI = M_PI;
inline ll pow(ll x, ll n, ll m) {
  ll r = 1;
  while (n > 0) {
    if ((n & 1) == 1)
      r = r * x % m;
    x = x * x % m;
    n >>= 1;
  }
  return r % m;
}
inline ll lcm(ll d1, ll d2) { return d1 / __gcd(d1, d2) * d2; }
#define chmax(a, b) a = max(a, b)

/*Coding Space*/
vector<int> ans;
vector<int> G[1001];
int main() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    G[a].pb(b);
  }
  rep(i, n) {
    int s = i + 1;
    bool used[1001] = {};
    vt<int> ss;
    ss.pb(s);
    queue<vector<int>> q;
    q.push(ss);
    while (!q.empty()) {
      auto now = q.front();
      q.pop();
      if (now.size() >= 2 && *now.rbegin() == s) {
        if (ans.size() == 0 || ans.size() > now.size())
          ans = now;
        break;
      }
      auto n = *now.rbegin();
      for (auto nn : G[n]) {
        if (used[nn] == 0) {
          now.pb(nn);
          q.push(now);
          now.pop_back();
          used[nn] = 1;
        }
      }
    }
  }
  if (ans.size() == 0)
    cout << -1 << endl;
  else {
    cout << ans.size() - 1 << endl;
    rep(i, ans.size() - 1) cout << ans[i] << endl;
  }
}