Problem p02902 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repp(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define pb push_back
#define ppb pop_back()
#define ALL(a) (a).begin(), (a).end()
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;

signed main() {
  int n, m;
  cin >> n >> m;
  vector<int> to[n];
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].pb(b);
  }
  bool ok = 0;
  vector<int> sta;
  rep(i, n) {
    bool bl = 0;
    vector<bool> vis(n, 0);
    vis[i] = 1;
    queue<int> q;
    q.push(i);
    while (!q.empty()) {
      if (bl)
        break;
      int v = q.front();
      q.pop();
      for (int u : to[v]) {
        if (u == i) {
          // exist loop
          sta.pb(i);
          ok = 1;
          bl = 1;
          break;
        }
        q.push(u);
        vis[u] = 1;
      }
    }
  }
  if (!ok) {
    cout << -1 << endl;
    return 0;
  }
  vector<P> vp;
  vector<int> v[n];
  for (int st : sta) {
    // st->stの最短経路を持って復元
    int d[n];
    rep(i, n) d[i] = 1001001;
    priority_queue<P, vector<P>, greater<P>> que;
    d[st] = 0;
    int okk = 0;
    que.push(P(0, st));
    int prev[n];
    while (!que.empty()) {
      if (okk == 1) {
        d[st] = 100100;
      }
      okk++;
      P p = que.top();
      que.pop();
      int v = p.second;
      if (d[v] < p.first)
        continue;
      for (int i = 0; i < to[v].size(); ++i) {
        int e = to[v][i];
        if (d[e] > d[v] + 1) {
          d[e] = d[v] + 1;
          prev[e] = v;
          que.push(P(d[e], e));
        }
      }
    }
    vp.pb({d[st], st});
    int stt = st;
    int cnt = d[st];
    rep(i, cnt) {
      v[stt].pb(prev[st]);
      st = prev[st];
    }
    reverse(ALL(v[stt]));
  }
  sort(ALL(vp));
  P p = vp[0];
  cout << p.fi << endl;
  for (int u : v[p.se]) {
    cout << ++u << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repp(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define pb push_back
#define ppb pop_back()
#define ALL(a) (a).begin(), (a).end()
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;

signed main() {
  int n, m;
  cin >> n >> m;
  vector<int> to[n];
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].pb(b);
  }
  bool ok = 0;
  vector<int> sta;
  rep(i, n) {
    bool bl = 0;
    vector<bool> vis(n, 0);
    vis[i] = 1;
    queue<int> q;
    q.push(i);
    while (!q.empty()) {
      if (bl)
        break;
      int v = q.front();
      q.pop();
      for (int u : to[v]) {
        if (u == i) {
          // exist loop
          sta.pb(i);
          ok = 1;
          bl = 1;
          break;
        }
        if (vis[u])
          continue;
        q.push(u);
        vis[u] = 1;
      }
    }
  }
  if (!ok) {
    cout << -1 << endl;
    return 0;
  }
  vector<P> vp;
  vector<int> v[n];
  for (int st : sta) {
    // st->stの最短経路を持って復元
    int d[n];
    rep(i, n) d[i] = 1001001;
    priority_queue<P, vector<P>, greater<P>> que;
    d[st] = 0;
    int okk = 0;
    que.push(P(0, st));
    int prev[n];
    while (!que.empty()) {
      if (okk == 1) {
        d[st] = 100100;
      }
      okk++;
      P p = que.top();
      que.pop();
      int v = p.second;
      if (d[v] < p.first)
        continue;
      for (int i = 0; i < to[v].size(); ++i) {
        int e = to[v][i];
        if (d[e] > d[v] + 1) {
          d[e] = d[v] + 1;
          prev[e] = v;
          que.push(P(d[e], e));
        }
      }
    }
    vp.pb({d[st], st});
    int stt = st;
    int cnt = d[st];
    rep(i, cnt) {
      v[stt].pb(prev[st]);
      st = prev[st];
    }
    reverse(ALL(v[stt]));
  }
  sort(ALL(vp));
  P p = vp[0];
  cout << p.fi << endl;
  for (int u : v[p.se]) {
    cout << ++u << endl;
  }
  return 0;
}