Problem p02902 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>

const int maxn = 1e3 + 10;

struct edge {
  int to, next;
} map[maxn];

int n, m, stk[maxn], top, val, ans[maxn], tp, nxt[maxn];
int head[maxn], cnt;
bool vis[maxn], flag, tag[maxn];

inline void add(int x, int y) {
  map[++cnt] = (edge){y, head[x]};
  head[x] = cnt;
  return;
}

void dfs(int x) {
  stk[++top] = x, vis[x] = tag[x] = true;
  for (register int i = head[x]; i; i = map[i].next) {
    int y = map[i].to;
    if (!vis[y])
      dfs(y);
    else if (tag[y]) {
      val = y, flag = true;
    }
    if (flag)
      return;
  }
  top--, vis[x] = false;
  return;
}

int main() {
  scanf("%d%d", &n, &m);
  for (register int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    add(x, y);
  }
  for (register int i = 1; i <= n; i++) {
    if (!tag[i]) {
      dfs(i);
      if (flag)
        break;
    }
  }
  if (!flag) {
    printf("-1");
    return 0;
  }
  int pos;
  for (register int i = 1; i <= top; i++)
    if (stk[i] == val)
      pos = i;
  nxt[stk[top]] = val;
  for (register int i = pos; i < top; i++)
    nxt[stk[i]] = stk[i + 1];
  for (register int i = 1; i <= n; i++) {
    if (!nxt[i])
      continue;
    for (register int j = head[i]; j; j = map[j].next) {
      int y = map[j].to;
      if (!nxt[y])
        continue;
      int p = i, q = nxt[i];
      while (q != y) {
        int r = nxt[q];
        nxt[p] = r;
        nxt[q] = 0;
        q = r;
      }
    }
  }
  int x;
  for (register int i = 1; i <= n; i++)
    if (nxt[i])
      x = i;
  int p = x;
  ans[++tp] = p;
  p = nxt[p];
  while (p != x) {
    ans[++tp] = p;
    p = nxt[p];
  }
  std::cout << tp << std::endl;
  for (register int i = 1; i <= tp; i++) {
    std::cout << ans[i] << std::endl;
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>

const int maxn = 1e6 + 10;

struct edge {
  int to, next;
} map[maxn];

int n, m, stk[maxn], top, val, ans[maxn], tp, nxt[maxn];
int head[maxn], cnt;
bool vis[maxn], flag, tag[maxn];

inline void add(int x, int y) {
  map[++cnt] = (edge){y, head[x]};
  head[x] = cnt;
  return;
}

void dfs(int x) {
  stk[++top] = x, vis[x] = tag[x] = true;
  for (register int i = head[x]; i; i = map[i].next) {
    int y = map[i].to;
    if (!vis[y])
      dfs(y);
    else if (tag[y]) {
      val = y, flag = true;
    }
    if (flag)
      return;
  }
  top--, vis[x] = false;
  return;
}

int main() {
  scanf("%d%d", &n, &m);
  for (register int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    add(x, y);
  }
  for (register int i = 1; i <= n; i++) {
    if (!tag[i]) {
      dfs(i);
      if (flag)
        break;
    }
  }
  if (!flag) {
    printf("-1");
    return 0;
  }
  int pos;
  for (register int i = 1; i <= top; i++)
    if (stk[i] == val)
      pos = i;
  nxt[stk[top]] = val;
  for (register int i = pos; i < top; i++)
    nxt[stk[i]] = stk[i + 1];
  for (register int i = 1; i <= n; i++) {
    if (!nxt[i])
      continue;
    for (register int j = head[i]; j; j = map[j].next) {
      int y = map[j].to;
      if (!nxt[y])
        continue;
      int p = i, q = nxt[i];
      while (q != y) {
        int r = nxt[q];
        nxt[p] = r;
        nxt[q] = 0;
        q = r;
      }
    }
  }
  int x;
  for (register int i = 1; i <= n; i++)
    if (nxt[i])
      x = i;
  int p = x;
  ans[++tp] = p;
  p = nxt[p];
  while (p != x) {
    ans[++tp] = p;
    p = nxt[p];
  }
  std::cout << tp << std::endl;
  for (register int i = 1; i <= tp; i++) {
    std::cout << ans[i] << std::endl;
  }
  return 0;
}