Problem p02902 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;

const int MAXN = 1024;
int vis[MAXN], parent[MAXN];
vector<int> adj[MAXN];
int nxt[MAXN];

void go(vector<int> cycle) {
  map<int, int> in_cycle;
  for (int i = 0; i < cycle.size(); i++) {
    in_cycle[cycle[i]] = i;
    // cerr << "in cycle: " << cycle[i]+1 << endl;
  }
  int u = cycle[0];
  // cerr << "adding: " << u + 1 << endl;
  queue<int> q;
  q.push(u);
  while (true) {
    int cur_pos = in_cycle[u];
    int best_pos = cur_pos;
    int node = -2;
    // cerr << "searching for nxt after " << u + 1 << endl;
    // cerr << "cur pos: " << cur_pos << endl;
    for (int v : adj[u]) {
      if (in_cycle.count(v) == 0)
        continue;
      int pos = in_cycle[v];
      // cerr << "trying: " << v+1 << ", pos: " << pos << endl;
      if (pos > cur_pos) {
        if (best_pos >= cur_pos && pos > best_pos) {
          best_pos = pos;
          node = v;
        }
      } else {
        if (best_pos >= cur_pos || pos > best_pos) {
          best_pos = pos;
          node = v;
        }
      }
    }
    // cerr << "next one is: " << node+1 << endl;
    if (best_pos > cur_pos) {
      q.push(node);
      u = node;
    } else {
      while (!q.empty() && in_cycle[q.front()] < best_pos)
        q.pop();
      assert(q.front() == node);
      break;
    }
  }
  cout << q.size() << endl;
  while (!q.empty()) {
    cout << q.front() + 1 << endl;
    q.pop();
  }
  exit(0);
}

void dfs(int u) {
  vis[u] = 1;
  bool here = false;
  int w;
  for (int v : adj[u]) {
    if (vis[v] == 1) {
      here = true;
      w = v;
      break;
    }
    parent[v] = u;
    dfs(v);
  }
  vector<int> cycle;
  if (here) {
    while (u != w) {
      cycle.push_back(u);
      u = parent[u];
    }
    cycle.push_back(w);
    reverse(cycle.begin(), cycle.end());
    go(cycle);
  }
  vis[u] = 2;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v);
  }
  fill(vis, vis + n, 0);
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      dfs(i);
    }
  }
  cout << -1 << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;

const int MAXN = 1024;
int vis[MAXN], parent[MAXN];
vector<int> adj[MAXN];
int nxt[MAXN];

void go(vector<int> cycle) {
  map<int, int> in_cycle;
  for (int i = 0; i < cycle.size(); i++) {
    in_cycle[cycle[i]] = i;
    // cerr << "in cycle: " << cycle[i]+1 << endl;
  }
  int u = cycle[0];
  // cerr << "adding: " << u + 1 << endl;
  queue<int> q;
  q.push(u);
  while (true) {
    int cur_pos = in_cycle[u];
    int best_pos = cur_pos;
    int node = -2;
    // cerr << "searching for nxt after " << u + 1 << endl;
    // cerr << "cur pos: " << cur_pos << endl;
    for (int v : adj[u]) {
      if (in_cycle.count(v) == 0)
        continue;
      int pos = in_cycle[v];
      // cerr << "trying: " << v+1 << ", pos: " << pos << endl;
      if (pos > cur_pos) {
        if (best_pos >= cur_pos && pos > best_pos) {
          best_pos = pos;
          node = v;
        }
      } else {
        if (best_pos >= cur_pos || pos > best_pos) {
          best_pos = pos;
          node = v;
        }
      }
    }
    // cerr << "next one is: " << node+1 << endl;
    if (best_pos > cur_pos) {
      q.push(node);
      u = node;
      for (int j = cur_pos + 1; j < best_pos; j++) {
        int v = cycle[j];
        in_cycle.erase(v);
      }
    } else {
      while (!q.empty() && in_cycle[q.front()] < best_pos)
        q.pop();
      assert(q.front() == node);
      break;
    }
  }
  cout << q.size() << endl;
  while (!q.empty()) {
    cout << q.front() + 1 << endl;
    q.pop();
  }
  exit(0);
}

void dfs(int u) {
  vis[u] = 1;
  bool here = false;
  int w;
  for (int v : adj[u]) {
    if (vis[v] == 1) {
      here = true;
      w = v;
      break;
    }
    parent[v] = u;
    dfs(v);
  }
  vector<int> cycle;
  if (here) {
    while (u != w) {
      cycle.push_back(u);
      u = parent[u];
    }
    cycle.push_back(w);
    reverse(cycle.begin(), cycle.end());
    go(cycle);
  }
  vis[u] = 2;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v);
  }
  fill(vis, vis + n, 0);
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      dfs(i);
    }
  }
  cout << -1 << endl;
  return 0;
}