Problem p02902 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for (int i = 0; i < n; ++i)

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n);
  vector<int> d(n);
  vector<pair<int, int>> e(m);
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    d[b]++;
    g[a].push_back(b);
    e[i] = {a, b};
  }
  queue<int> q;
  REP(i, n) {
    if (d[i] == 0) {
      q.push(i);
    }
  }
  while (!q.empty()) {
    int p = q.front();
    q.pop();
    REP(i, g[p].size()) {
      if (--d[g[p][i]] == 0)
        q.push(g[p][i]);
    }
  }
  if (*max_element(d.begin(), d.end()) == 0) {
    cout << -1 << endl;
    return 0;
  }

  vector<vector<int>> dist(n, vector<int>(n, 1e9 + 1)),
      prev(n, vector<int>(n, -1));
  REP(i, n) {
    dist[i][i] = 0;
    q.push(i);
    while (!q.empty()) {
      int p = q.front();
      q.pop();
      for (auto a : g[p]) {
        if (dist[i][a] > dist[i][p] + 1) {
          q.push(a);
          dist[i][a] = dist[i][p] + 1;
          prev[i][a] = p;
        }
      }
    }
  }

  vector<int> res;
  REP(i, m) {
    if (dist[e[i].second][e[i].first] != 1e9 + 1) {
      int pos = e[i].first;
      while (pos != e[i].second) {
        res.push_back(pos);
        pos = prev[e[i].second][pos];
      }
      res.push_back(pos);
    }
  }
  reverse(res.begin(), res.end());
  set<int> st(res.begin(), res.end());
  {
    set<pair<int, int>> s(e.begin(), e.end());
    REP(i, res.size()) { assert(s.count({res[i], res[(i + 1) % res.size()]})); }
  }
  while (1) {
    bool f = false;
    REP(i, m) {
      if (st.count(e[i].first) && st.count(e[i].second)) {

        int a = find(res.begin(), res.end(), e[i].first) - res.begin(),
            b = find(res.begin(), res.end(), e[i].second) - res.begin();
        if ((a + 1) % res.size() == b)
          continue;
        vector<int> next;
        if (a < b) {
          for (int i = 0; i <= a; ++i)
            next.push_back(res[i]);
          for (int i = b; i < res.size(); ++i)
            next.push_back(res[i]);
        } else {
          for (int i = b; i <= a; ++i)
            next.push_back(res[i]);
        }
        res = next;
        st = set<int>(res.begin(), res.end());
        f = true;
      }
    }
    if (!f)
      break;
  }
  cout << res.size() << endl;
  for (auto a : res)
    cout << a + 1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for (int i = 0; i < n; ++i)

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n);
  vector<int> d(n);
  vector<pair<int, int>> e(m);
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    d[b]++;
    g[a].push_back(b);
    e[i] = {a, b};
  }
  queue<int> q;
  REP(i, n) {
    if (d[i] == 0) {
      q.push(i);
    }
  }
  while (!q.empty()) {
    int p = q.front();
    q.pop();
    REP(i, g[p].size()) {
      if (--d[g[p][i]] == 0)
        q.push(g[p][i]);
    }
  }
  if (*max_element(d.begin(), d.end()) == 0) {
    cout << -1 << endl;
    return 0;
  }

  vector<vector<int>> dist(n, vector<int>(n, 1e9 + 1)),
      prev(n, vector<int>(n, -1));
  REP(i, n) {
    dist[i][i] = 0;
    q.push(i);
    while (!q.empty()) {
      int p = q.front();
      q.pop();
      for (auto a : g[p]) {
        if (dist[i][a] > dist[i][p] + 1) {
          q.push(a);
          dist[i][a] = dist[i][p] + 1;
          prev[i][a] = p;
        }
      }
    }
  }

  vector<int> res;
  REP(i, m) {
    if (dist[e[i].second][e[i].first] != 1e9 + 1) {
      int pos = e[i].first;
      while (pos != e[i].second) {
        res.push_back(pos);
        pos = prev[e[i].second][pos];
      }
      res.push_back(pos);
      break;
    }
  }
  reverse(res.begin(), res.end());
  set<int> st(res.begin(), res.end());
  {
    set<pair<int, int>> s(e.begin(), e.end());
    REP(i, res.size()) { assert(s.count({res[i], res[(i + 1) % res.size()]})); }
  }
  while (1) {
    bool f = false;
    REP(i, m) {
      if (st.count(e[i].first) && st.count(e[i].second)) {

        int a = find(res.begin(), res.end(), e[i].first) - res.begin(),
            b = find(res.begin(), res.end(), e[i].second) - res.begin();
        if ((a + 1) % res.size() == b)
          continue;
        vector<int> next;
        if (a < b) {
          for (int i = 0; i <= a; ++i)
            next.push_back(res[i]);
          for (int i = b; i < res.size(); ++i)
            next.push_back(res[i]);
        } else {
          for (int i = b; i <= a; ++i)
            next.push_back(res[i]);
        }
        res = next;
        st = set<int>(res.begin(), res.end());
        f = true;
      }
    }
    if (!f)
      break;
  }
  cout << res.size() << endl;
  for (auto a : res)
    cout << a + 1 << endl;
}