Problem p02902 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, m;
struct edge {
  int v, nxt;
} e[2019][2];
int head[2019][2], in[2019], out[2019], tot[2] = {1, 1}, vst[2019];
void ae(int u, int v, int f) {
  if (!f) {
    in[v]++;
    out[u]++;
  }
  e[tot[f]][f] = {v, head[u][f]};
  head[u][f] = tot[f]++;
}
queue<int> q;
int ans[2000], tmp[2000], ptr = 2000, ptr1 = 0;
int kkk = 2;
void dfs(int u, int len) {
  if (len >= ptr)
    return;
  vst[u] = kkk;
  tmp[len] = u;
  bool ok = 0;
  for (int i = head[u][0]; i; i = e[i][0].nxt) {
    int v = e[i][0].v;
    if (vst[v] != 1 && vst[v] != kkk) {
      dfs(v, len + 1);
      ok = 1;
    }
  }
  if (!ok) {
    if (len < ptr) {
      ptr = len + 1;
      for (int i = 0; i < ptr; i++) {
        ans[i] = tmp[i];
      }
    }
  }
  vst[u] = kkk - 1;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    ae(u, v, 0);
    ae(v, u, 1);
  }
  int visit = 0;
  for (int i = 1; i <= n; i++) {
    if (in[i] == 0 || out[i] == 0)
      q.push(i), vst[i] = 1;
  }
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    if (in[u] == 0) {
      for (int i = head[u][0]; i; i = e[i][0].nxt) {
        int v = e[i][0].v;
        in[v]--;
        if (in[v] == 0 && !vst[v]) {
          vst[v] = 1;
          q.push(v);
        }
      }
    } else {
      for (int i = head[u][1]; i; i = e[i][1].nxt) {
        int v = e[i][1].v;
        out[v]--;
        if (out[v] == 0 && !vst[v]) {
          vst[v] = 1;
          q.push(v);
        }
      }
    }
  }
  for (int i = 1; i <= n; i++)
    if (in[i] == out[i] && in[i] == 0)
      visit++;
  if (visit == n) {
    cout << -1 << endl;
    return 0;
  }
  int x = 1;
  bool ok = 0;
  for (; x <= n; x++) {
    if (vst[x] != 1) {
      kkk++;
      dfs(x, 0);
    }
  }
  if (ptr == 0 || ptr == 2000) {
    cout << -1 << endl;
    return 0;
  }
  cout << ptr << endl;
  for (int i = 0; i < ptr; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, m;
struct edge {
  int v, nxt;
} e[2019][2];
int head[2019][2], in[2019], out[2019], tot[2] = {1, 1}, vst[2019];
void ae(int u, int v, int f) {
  if (!f) {
    in[v]++;
    out[u]++;
  }
  e[tot[f]][f] = {v, head[u][f]};
  head[u][f] = tot[f]++;
}
queue<int> q;
int ans[2000], tmp[2000], ptr = 2000, ptr1 = 0;
int kkk = 2;
void dfs(int u, int len) {
  if (len >= ptr)
    return;
  vst[u] = kkk;
  tmp[len] = u;
  bool ok = 0;
  for (int i = head[u][0]; i; i = e[i][0].nxt) {
    int v = e[i][0].v;
    if (vst[v] == kkk)
      break;
    if (vst[v] != 1 && vst[v] != kkk) {
      dfs(v, len + 1);
      ok = 1;
    }
  }
  if (!ok) {
    if (len < ptr) {
      ptr = len + 1;
      for (int i = 0; i < ptr; i++) {
        ans[i] = tmp[i];
      }
    }
  }
  vst[u] = kkk - 1;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    ae(u, v, 0);
    ae(v, u, 1);
  }
  int visit = 0;
  for (int i = 1; i <= n; i++) {
    if (in[i] == 0 || out[i] == 0)
      q.push(i), vst[i] = 1;
  }
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    if (in[u] == 0) {
      for (int i = head[u][0]; i; i = e[i][0].nxt) {
        int v = e[i][0].v;
        in[v]--;
        if (in[v] == 0 && !vst[v]) {
          vst[v] = 1;
          q.push(v);
        }
      }
    } else {
      for (int i = head[u][1]; i; i = e[i][1].nxt) {
        int v = e[i][1].v;
        out[v]--;
        if (out[v] == 0 && !vst[v]) {
          vst[v] = 1;
          q.push(v);
        }
      }
    }
  }
  for (int i = 1; i <= n; i++)
    if (in[i] == out[i] && in[i] == 0)
      visit++;
  if (visit == n) {
    cout << -1 << endl;
    return 0;
  }
  int x = 1;
  bool ok = 0;
  for (; x <= n; x++) {
    if (vst[x] != 1) {
      kkk++;
      dfs(x, 0);
    }
  }
  if (ptr == 0 || ptr == 2000) {
    cout << -1 << endl;
    return 0;
  }
  cout << ptr << endl;
  for (int i = 0; i < ptr; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}