Problem p02902 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int N = 2009;
int n, m;
vector<int> g[N];
int dfn[N], low[N], num, vis[N], co[N], col;
stack<int> S;
int siz[N];
void tarjan(int u) {
  // cout<<u<<endl;
  dfn[u] = low[u] = ++num;
  vis[u] = 1;
  S.push(u);
  for (auto v : g[u]) {
    if (!dfn[v])
      tarjan(v), low[u] = min(low[u], low[v]);
    else if (vis[v])
      low[u] = min(low[u], dfn[v]);
  }
  if (dfn[u] == low[u]) {
    co[u] = ++col;
    siz[col]++;
    while (S.top() != u) {
      co[S.top()] = col;
      vis[S.top()] = 0;
      siz[col]++;
      S.pop();
    }
    vis[u] = 0;
    S.pop();
  }
}
// bool vv[N];
vector<int> ans;
vector<int> uu;
int pre[N], vv[N];
void print(int u, int fa) {
  ans.clear();
  while (1) {
    ans.push_back(u);
    u = pre[u];
    if (u == fa)
      break;
  }
  ans.push_back(fa);
  if (uu.size() == 0 || uu.size() > ans.size())
    uu = ans;
  // cout<<uu.size()<<endl;
  // cout<<ans.size()<<endl;
  // for(auto v:ans)cout<<v<<endl;
  //  exit(0);
}
void bfs(int u, int fa) {
  queue<int> q;
  q.push(u);
  vv[u] = 1;
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (auto v : g[now])
      if (vv[v] == 1) {
        vv[v] = 2;
        print(now, v);
      } else {
        if (vv[v] == 2)
          continue;
        if (co[v] != fa)
          continue;
        //    if(vv[v])memset(vv,0,sizeof(vv)),bfs(now,fa);
        vv[v] = 1;
        q.push(v);
        pre[v] = now;
      }
  }
}
int main() {
  // freopen("tt.in","r",stdin),freopen("tt.out","w",stdout);
  cin >> n >> m;
  for (int i = 1, u, v; i <= m; i++)
    cin >> u >> v, g[u].push_back(v);
  for (int i = 1; i <= n; i++)
    if (!dfn[i])
      tarjan(i);
  for (int i = 1; i <= col; i++)
    if (siz[i] > 1) {
      // cout<<siz[i]<<endl;
      for (int j = 1; j <= n; j++)
        if (co[j] == i) {
          bfs(j, i);
          break;
        }
      break;
      // return 0;
    }
  if (uu.empty()) {
    cout << -1 << endl;
    return 0;
  }
  cout << uu.size() << endl;
  for (auto i : uu)
    cout << i << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 2009;
int n, m;
vector<int> g[N];
int dfn[N], low[N], num, vis[N], co[N], col;
stack<int> S;
int siz[N];
void tarjan(int u) {
  // cout<<u<<endl;
  dfn[u] = low[u] = ++num;
  vis[u] = 1;
  S.push(u);
  for (auto v : g[u]) {
    if (!dfn[v])
      tarjan(v), low[u] = min(low[u], low[v]);
    else if (vis[v])
      low[u] = min(low[u], dfn[v]);
  }
  if (dfn[u] == low[u]) {
    co[u] = ++col;
    siz[col]++;
    while (S.top() != u) {
      co[S.top()] = col;
      vis[S.top()] = 0;
      siz[col]++;
      S.pop();
    }
    vis[u] = 0;
    S.pop();
  }
}
// bool vv[N];
vector<int> ans;
vector<int> uu;
int pre[N], vv[N];
void print(int u, int fa) {
  ans.clear();
  while (1) {
    ans.push_back(u);
    u = pre[u];
    if (u == fa)
      break;
    if (ans.size() > 1000)
      break;
  }
  ans.push_back(fa);
  if (uu.size() == 0 || uu.size() > ans.size())
    uu = ans;
  // cout<<uu.size()<<endl;
  // cout<<ans.size()<<endl;
  // for(auto v:ans)cout<<v<<endl;
  //  exit(0);
}
void bfs(int u, int fa) {
  queue<int> q;
  q.push(u);
  vv[u] = 1;
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (auto v : g[now])
      if (vv[v] == 1) {
        vv[v] = 2;
        print(now, v);
      } else {
        if (vv[v] == 2)
          continue;
        if (co[v] != fa)
          continue;
        //    if(vv[v])memset(vv,0,sizeof(vv)),bfs(now,fa);
        vv[v] = 1;
        q.push(v);
        pre[v] = now;
      }
  }
}
int main() {
  // freopen("tt.in","r",stdin),freopen("tt.out","w",stdout);
  cin >> n >> m;
  for (int i = 1, u, v; i <= m; i++)
    cin >> u >> v, g[u].push_back(v);
  for (int i = 1; i <= n; i++)
    if (!dfn[i])
      tarjan(i);
  for (int i = 1; i <= col; i++)
    if (siz[i] > 1) {
      // cout<<siz[i]<<endl;
      for (int j = 1; j <= n; j++)
        if (co[j] == i) {
          bfs(j, i);
          break;
        }
      break;
      // return 0;
    }
  if (uu.empty()) {
    cout << -1 << endl;
    return 0;
  }
  cout << uu.size() << endl;
  for (auto i : uu)
    cout << i << endl;
  return 0;
}