Problem p02902 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  cout.precision(10);                                                          \
  cout << fixed;
#define int long long int
#define pb push_back
#define endl "\n"
#define all(a) a.begin(), a.end()
#define N 1009
#define M 1000009
#define mod 1000000007
int powi(int a, int b) {
  if (b == 0)
    return 1;
  int see = powi(a, b / 2);
  see *= see;
  see %= mod;
  if (b % 2)
    see *= a;
  see %= mod;
  return see;
}

int n, m;
int a, b;
vector<int> v[N];
bool edge[N][N];
vector<pair<int, int>> ed;
int par[N];
bool vis[N];
vector<int> globl;

void pre() { memset(vis, 0, sizeof(vis)); }

int32_t main() {
  IOS;
  pre();
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    globl.push_back(-1);
  }
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    v[a].push_back(b);
    edge[a][b] = 1;
    ed.push_back({a, b});
    if (edge[b][a] == 1) {
      cout << "2\n" << a << endl << b << endl;
      return 0;
    }
  }
  for (auto i : ed) {
    // bfs from second
    pre();
    bool cycle = 0;
    queue<int> q;
    q.push(i.second);
    while (!q.empty()) {
      int tp = q.front();
      vis[tp] = 1;
      for (auto j : v[tp]) {
        if (vis[j] == 1 && j == i.first) {
          while (!q.empty())
            q.pop();
          par[j] = tp;
          break;
        }
        par[j] = tp;
        q.push(j);
      }
      if (q.size() > 0)
        q.pop();
    }
    if (vis[i.first] == 0) {
      continue;
    }
    vector<int> ans;
    int temp = i.first;
    while (temp != i.second) {
      ans.push_back(temp);
      temp = par[temp];
    }
    ans.push_back(i.second);
    if (ans.size() <= globl.size()) {
      globl = ans;
    }
  }
  if (globl[0] == -1) {
    cout << "-1\n";
    return 0;
  }
  cout << globl.size() << endl;
  for (auto i : globl) {
    cout << i << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  cout.precision(10);                                                          \
  cout << fixed;
#define int long long int
#define pb push_back
#define endl "\n"
#define all(a) a.begin(), a.end()
#define N 1009
#define M 1000009
#define mod 1000000007
int powi(int a, int b) {
  if (b == 0)
    return 1;
  int see = powi(a, b / 2);
  see *= see;
  see %= mod;
  if (b % 2)
    see *= a;
  see %= mod;
  return see;
}

int n, m;
int a, b;
vector<int> v[N];
bool edge[N][N];
vector<pair<int, int>> ed;
int par[N];
bool vis[N];
vector<int> globl;

void pre() { memset(vis, 0, sizeof(vis)); }

int32_t main() {
  IOS;
  pre();
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    globl.push_back(-1);
  }
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    v[a].push_back(b);
    edge[a][b] = 1;
    ed.push_back({a, b});
    if (edge[b][a] == 1) {
      cout << "2\n" << a << endl << b << endl;
      return 0;
    }
  }
  for (auto i : ed) {
    // bfs from second
    pre();
    bool cycle = 0;
    queue<int> q;
    q.push(i.second);
    while (!q.empty()) {
      int tp = q.front();
      vis[tp] = 1;
      for (auto j : v[tp]) {
        if (vis[j] == 1 && j == i.first) {
          while (!q.empty())
            q.pop();
          par[j] = tp;
          break;
        }
        if (vis[j] == 1)
          continue;
        par[j] = tp;
        q.push(j);
      }
      if (q.size() > 0)
        q.pop();
    }
    if (vis[i.first] == 0) {
      continue;
    }
    vector<int> ans;
    int temp = i.first;
    while (temp != i.second) {
      ans.push_back(temp);
      temp = par[temp];
    }
    ans.push_back(i.second);
    if (ans.size() <= globl.size()) {
      globl = ans;
    }
  }
  if (globl[0] == -1) {
    cout << "-1\n";
    return 0;
  }
  cout << globl.size() << endl;
  for (auto i : globl) {
    cout << i << endl;
  }
  return 0;
}