Problem p02902 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define REP(i, k, n) for (long long i = k; i < (long long)(n); i++)
#define all(a) a.begin(), a.end()
#define eb emplace_back
#define pb push_back
#define lb(v, k) (lower_bound(all(v), k) - v.begin())
#define chmin(x, y)                                                            \
  if (x > y)                                                                   \
  x = y
#define chmax(x, y)                                                            \
  if (x < y)                                                                   \
  x = y
typedef long long ll;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> PP;
typedef priority_queue<ll> PQ;
typedef priority_queue<ll, vector<ll>, greater<ll>> SPQ;
using vi = vector<ll>;
using vvi = vector<vector<ll>>;
using vc = vector<char>;
using vvc = vector<vector<char>>;
const ll inf = 100100100100;
const int Inf = 1001001001;
const int mod = 1000000007;
const double pi = 3.141592653589793;

ll to[1005];
int dfs(ll i, vvi v) {
  for (ll x : v[i]) {
    to[i] = x;
    if (to[x] != -1)
      return i;
    int k = dfs(x, v);
    if (k != -1)
      return k;
  }
  to[i] = -1;
  return -1;
}

int main() {
  ll n, m;
  cin >> n >> m;
  rep(i, n) to[i] = -1;
  vvi v(n);
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].pb(b);
  }
  rep(i, n) {
    ll k = dfs(i, v);
    if (k != -1) {
      vi ito(n);
      ll u = k;
      ito[u] = to[u];
      u = to[k];
      while (u != k) {
        ito[u] = to[u];
        u = to[u];
      }
      while (true) {
        vi d;
        rep(i, n) if (ito[i] != -1) d.pb(i);
        ll from = inf, go;
        for (ll j : d) {
          bool b = false;
          for (ll x : v[j]) {
            if (x != ito[j] && ito[x] != -1) {
              from = j;
              go = x;
              b = true;
              break;
            }
          }
          if (b)
            break;
        }
        if (from == inf) {
          cout << d.size() << endl;
          rep(i, d.size()) cout << d[i] + 1 << endl;
          return 0;
        }
        ll j = from;
        while (true) {
          ll k = ito[j];
          ito[j] = -1;
          j = k;
          if (j == go)
            break;
        }
        ito[from] = go;
      }
    }
  }
  cout << -1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define REP(i, k, n) for (long long i = k; i < (long long)(n); i++)
#define all(a) a.begin(), a.end()
#define eb emplace_back
#define pb push_back
#define lb(v, k) (lower_bound(all(v), k) - v.begin())
#define chmin(x, y)                                                            \
  if (x > y)                                                                   \
  x = y
#define chmax(x, y)                                                            \
  if (x < y)                                                                   \
  x = y
typedef long long ll;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> PP;
typedef priority_queue<ll> PQ;
typedef priority_queue<ll, vector<ll>, greater<ll>> SPQ;
using vi = vector<ll>;
using vvi = vector<vector<ll>>;
using vc = vector<char>;
using vvc = vector<vector<char>>;
const ll inf = 100100100100;
const int Inf = 1001001001;
const int mod = 1000000007;
const double pi = 3.141592653589793;

ll to[1005];
int dfs(ll i, vvi v) {
  for (ll x : v[i]) {
    to[i] = x;
    if (to[x] != -1)
      return i;
    int k = dfs(x, v);
    if (k != -1)
      return k;
  }
  to[i] = -1;
  return -1;
}

int main() {
  ll n, m;
  cin >> n >> m;
  rep(i, n) to[i] = -1;
  vvi v(n);
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].pb(b);
  }
  rep(i, n) {
    ll k = dfs(i, v);
    if (k != -1) {
      vi ito(n, -1);
      ll u = k;
      ito[u] = to[u];
      u = to[k];
      while (u != k) {
        ito[u] = to[u];
        u = to[u];
      }
      while (true) {
        vi d;
        rep(i, n) if (ito[i] != -1) d.pb(i);
        ll from = inf, go;
        for (ll j : d) {
          bool b = false;
          for (ll x : v[j]) {
            if (x != ito[j] && ito[x] != -1) {
              from = j;
              go = x;
              b = true;
              break;
            }
          }
          if (b)
            break;
        }
        if (from == inf) {
          cout << d.size() << endl;
          rep(i, d.size()) cout << d[i] + 1 << endl;
          return 0;
        }
        ll j = from;
        while (true) {
          ll k = ito[j];
          ito[j] = -1;
          j = k;
          if (j == go)
            break;
        }
        ito[from] = go;
      }
    }
  }
  cout << -1 << endl;
}