Problem p02902 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm> // minmax, sort, swap
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>     // printf, scanf
#include <deque>      // deque
#include <functional> // function<void(int)>
#include <iomanip>    // cout<<setprecision(n)
#include <iostream>   // cin, cout, cerr
#include <map>        // key-value pairs sorted by keys
#include <numeric>    // iota
#include <queue>      // queue, priority_queue
#include <set>        // set
#include <string>     // string, stoi, to_string
#include <vector>     // vector

#ifdef DEBUG
#include "debug.hpp"
#else
#define debug(...)
#endif

#define int long long // at least int64 > 9*10^18
#define EL '\n'
#define rep(i, n) for (int i = 0; i < (n); i++)
#define print(i) std::cout << (i) << '\n'
#define all(v) (v).begin(), (v).end()
/* libraries */

signed main() {
  int n, m;
  std::cin >> n >> m;
  std::vector<std::vector<int>> g(n);
  rep(i, m) {
    int a, b;
    std::cin >> a >> b;
    a--;
    b--;
    g[a].emplace_back(b);
  }
  // find cycle
  std::vector<int> cycle;
  {
    std::vector<int> went(n, -1);
    auto dfs = [&](auto f, int i) -> int {
      for (auto to : g[i]) {
        // cycle
        if (went[to] == went[i]) {
          cycle.emplace_back(i);
          return to;
        }
        if (went[to] != -1)
          continue;
        went[to] = went[i];
        int x = f(f, to);
        if (x == -2)
          return -2;
        if (x != -1) {
          cycle.emplace_back(i);
          if (x == i)
            return -2;
          return x;
        }
      }
      return -1;
    };
    rep(i, n) {
      went[i] = i;
      if (dfs(dfs, i) == -2)
        break;
    }
  }
  if (cycle.size() == 0) {
    print(-1);
    return 0;
  }
  while (true) {
    debug(cycle);
    std::set<int> cs(all(cycle));
    std::set<std::pair<int, int>> set;
    rep(i, n) if (cs.count(i)) for (auto to : g[i]) if (cs.count(to))
        set.emplace(i, to);
    debug(set.size());
    int o = set.size();
    rep(i, cycle.size())
        set.erase(std::make_pair(cycle[(i + 1) % cycle.size()], cycle[i]));
    assert(o - cycle.size() == set.size());
    debug(set.size());
    if (set.empty())
      break;
    auto e = set.begin();
    int s = e->first, t = e->second;
    std::vector<int> went(n, 1);
    for (auto x : cycle)
      went[x] = -1;
    cycle.clear();
    auto dfs = [&](auto f, int i) -> int {
      if (i == s) {
        cycle.emplace_back(i);
        return 0;
      }
      for (auto to : g[i]) {
        if (went[to] != -1)
          continue;
        went[to] = went[i];
        int x = f(f, to);
        if (x != -1) {
          cycle.emplace_back(i);
          return x;
        }
      }
      return -1;
    };
    went[t] = 0;
    dfs(dfs, t);
  }
  if (cycle.size() == 0) {
    print(-1);
    return 0;
  }
  print(cycle.size());
  for (auto x : cycle)
    print(x + 1);

  return 0;
}


Fixed submission:
#include <algorithm> // minmax, sort, swap
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>     // printf, scanf
#include <deque>      // deque
#include <functional> // function<void(int)>
#include <iomanip>    // cout<<setprecision(n)
#include <iostream>   // cin, cout, cerr
#include <map>        // key-value pairs sorted by keys
#include <numeric>    // iota
#include <queue>      // queue, priority_queue
#include <set>        // set
#include <string>     // string, stoi, to_string
#include <vector>     // vector

#ifdef DEBUG
#include "debug.hpp"
#else
#define debug(...)
#endif

#define int long long // at least int64 > 9*10^18
#define EL '\n'
#define rep(i, n) for (int i = 0; i < (n); i++)
#define print(i) std::cout << (i) << '\n'
#define all(v) (v).begin(), (v).end()
/* libraries */

signed main() {
  int n, m;
  std::cin >> n >> m;
  std::vector<std::vector<int>> g(n);
  rep(i, m) {
    int a, b;
    std::cin >> a >> b;
    a--;
    b--;
    g[a].emplace_back(b);
  }
  // find cycle
  std::vector<int> cycle;
  {
    std::vector<int> went(n, -1);
    auto dfs = [&](auto f, int i) -> int {
      for (auto to : g[i]) {
        // cycle
        if (went[to] == went[i]) {
          cycle.emplace_back(i);
          return to;
        }
        if (went[to] != -1)
          continue;
        went[to] = went[i];
        int x = f(f, to);
        if (x == -2)
          return -2;
        if (x != -1) {
          cycle.emplace_back(i);
          if (x == i)
            return -2;
          return x;
        }
      }
      went[i] = -2;
      return -1;
    };
    rep(i, n) {
      went[i] = i;
      if (dfs(dfs, i) == -2)
        break;
    }
  }
  if (cycle.size() == 0) {
    print(-1);
    return 0;
  }
  while (true) {
    debug(cycle);
    std::set<int> cs(all(cycle));
    std::set<std::pair<int, int>> set;
    rep(i, n) if (cs.count(i)) for (auto to : g[i]) if (cs.count(to))
        set.emplace(i, to);
    debug(set.size());
    int o = set.size();
    rep(i, cycle.size())
        set.erase(std::make_pair(cycle[(i + 1) % cycle.size()], cycle[i]));
    assert(o - cycle.size() == set.size());
    debug(set.size());
    if (set.empty())
      break;
    auto e = set.begin();
    int s = e->first, t = e->second;
    std::vector<int> went(n, 1);
    for (auto x : cycle)
      went[x] = -1;
    cycle.clear();
    auto dfs = [&](auto f, int i) -> int {
      if (i == s) {
        cycle.emplace_back(i);
        return 0;
      }
      for (auto to : g[i]) {
        if (went[to] != -1)
          continue;
        went[to] = went[i];
        int x = f(f, to);
        if (x != -1) {
          cycle.emplace_back(i);
          return x;
        }
      }
      return -1;
    };
    went[t] = 0;
    dfs(dfs, t);
  }
  if (cycle.size() == 0) {
    print(-1);
    return 0;
  }
  print(cycle.size());
  for (auto x : cycle)
    print(x + 1);

  return 0;
}