Problem p02902 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define hhh printf("hhh\n")
#define see(x) (cerr << (#x) << '=' << (x) << endl)
using namespace std;
typedef long long ll;
typedef pair<int, int> pr;
inline int read() {
  int x = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x;
}

const int maxn = 1e3 + 10;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-7;

int N, M;
int pre[maxn], dis[maxn], vis[maxn];
int head[maxn], to[maxn], nxt[maxn], tot;
int ans[maxn], cnt = inf, res[maxn], cur;

inline void add_edge(int u, int v) {
  ++tot;
  to[tot] = v;
  nxt[tot] = head[u];
  head[u] = tot;
}

int main() {
  // ios::sync_with_stdio(false); cin.tie(0);
  N = read(), M = read();
  for (int i = 1; i <= M; ++i) {
    int u = read(), v = read();
    add_edge(u, v);
  }
  for (int i = 1; i <= N; ++i) {
    memset(vis, 0, sizeof(vis));
    int st = i;
    vis[st] = 1;
    queue<int> q;
    q.push(st);
    while (q.size()) {
      int u = q.front();
      q.pop();
      for (int i = head[u]; i; i = nxt[i]) {
        if (vis[to[i]])
          continue;
        int v = to[i];
        dis[v] = dis[u] + 1;
        vis[v] = 1;
        pre[v] = u;
        q.push(v);
      }
    }
    for (int i = 1; i <= N; ++i) {
      if (i == st || !vis[i])
        continue;
      cur = 0;
      for (int j = head[i]; j; j = nxt[j]) {
        if (to[j] == st) {
          int v = i;
          while (v != st)
            res[++cur] = v, v = pre[v];
          res[++cur] = st;
          if (cur < cnt) {
            cnt = cur;
            for (int i = 1; i <= cur; ++i)
              ans[i] = res[i];
          }
          break;
        }
      }
    }
  }
  if (cnt == inf)
    printf("-1\n");
  else {
    cout << cnt << endl;
    for (int i = 1; i <= cnt; ++i)
      printf("%d\n", ans[i]);
  }
}

Fixed submission:
#include "bits/stdc++.h"
#define hhh printf("hhh\n")
#define see(x) (cerr << (#x) << '=' << (x) << endl)
using namespace std;
typedef long long ll;
typedef pair<int, int> pr;
inline int read() {
  int x = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x;
}

const int maxn = 1e3 + 10;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-7;

int N, M;
int pre[maxn], dis[maxn], vis[maxn];
int head[maxn], to[maxn * 2], nxt[maxn * 2], tot;
int ans[maxn], cnt = inf, res[maxn], cur;

inline void add_edge(int u, int v) {
  ++tot;
  to[tot] = v;
  nxt[tot] = head[u];
  head[u] = tot;
}

int main() {
  // ios::sync_with_stdio(false); cin.tie(0);
  N = read(), M = read();
  for (int i = 1; i <= M; ++i) {
    int u = read(), v = read();
    add_edge(u, v);
  }
  for (int i = 1; i <= N; ++i) {
    memset(vis, 0, sizeof(vis));
    int st = i;
    vis[st] = 1;
    queue<int> q;
    q.push(st);
    while (q.size()) {
      int u = q.front();
      q.pop();
      for (int i = head[u]; i; i = nxt[i]) {
        if (vis[to[i]])
          continue;
        int v = to[i];
        dis[v] = dis[u] + 1;
        vis[v] = 1;
        pre[v] = u;
        q.push(v);
      }
    }
    for (int i = 1; i <= N; ++i) {
      if (i == st || !vis[i])
        continue;
      cur = 0;
      for (int j = head[i]; j; j = nxt[j]) {
        if (to[j] == st) {
          int v = i;
          while (v != st)
            res[++cur] = v, v = pre[v];
          res[++cur] = st;
          if (cur < cnt) {
            cnt = cur;
            for (int i = 1; i <= cur; ++i)
              ans[i] = res[i];
          }
          break;
        }
      }
    }
  }
  if (cnt == inf)
    printf("-1\n");
  else {
    cout << cnt << endl;
    for (int i = 1; i <= cnt; ++i)
      printf("%d\n", ans[i]);
  }
}