Problem p02902 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define INF 100000000
#define YJ 1145141919
#define INF_INT_MAX 2147483647
#define INF_LL 9223372036854775
#define INF_LL_MAX 9223372036854775807
#define EPS 1e-10
#define MOD 1000000007
#define MOD9 998244353
#define Pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define LD long double

#define int long long

using II = pair<int, int>;

int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }
int extgcd(int a, int b, int &x, int &y) {
  int g = a;
  x = 1;
  y = 0;
  if (b != 0)
    g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())

const int MAX_N = 1005;
const int MAX_M = 2005;

int N, M;
vector<int> Edge[MAX_N];

int used[MAX_N][2];
vector<int> ans;

int dfs(int pos, int depth = 1) {
  if (used[pos][1]) {
    return -1;
  }
  if (used[pos][0]) {
    int index = used[pos][0] - 1;
    return pos;
  }

  used[pos][0] = depth;
  ans.push_back(pos);
  for (int next : Edge[pos]) {
    int ret = dfs(next, depth + 1);
    if (ret != -1) {
      return ret;
    }
  }
  used[pos][0] = false;
  ans.pop_back();
  used[pos][1] = true;

  return -1;
}

bool edgeFlag[MAX_N][MAX_N];
bool used3[MAX_N];
int dist[MAX_N];
int rireki[MAX_N];

signed main() {
  cin >> N >> M;
  REP(m, M) {
    int a, b;
    cin >> a >> b;
    Edge[a].push_back(b);
    edgeFlag[a][b] = true;
  }

  REP(n, N + 1) {
    int ret = dfs(n);
    if (ret == -1)
      continue;
    else {
      int k = 0;
      REP(n, ans.size()) {
        if (ans[k] == ret) {
          k = n;
          break;
        }
      }

      FOR(n, k, ans.size()) { used3[ans[n]] = true; }

      vector<int> ans2;
      int dd = INF_LL;

      FOR(n, k, ans.size()) {
        memset(dist, -1, sizeof(dist));
        memset(rireki, -1, sizeof(rireki));
        queue<int> que;
        dist[ans[n]] = 0;
        que.push(ans[n]);
        int pre = -1;
        while (que.size() && pre == -1) {
          int pos = que.front();
          que.pop();
          for (int next : Edge[pos]) {
            if (used3[next] && dist[next] == -1) {
              dist[next] = dist[pos] + 1;
              que.push(next);
              rireki[next] = pos;
            }
            if (next == ans[n]) {
              pre = pos;
              break;
            }
          }
        }

        if (dd > dist[pre]) {
          dd = dist[pre];
          ans2.clear();
          ans2.push_back(pre);
          while (pre != ans[n]) {
            pre = rireki[pre];
            ans2.push_back(pre);
          }
        }
      }

      cout << ans2.size() << endl;
      for (int a : ans2) {
        cout << a << endl;
      }

      return 0;
    }
  }
  cout << -1 << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define INF 100000000
#define YJ 1145141919
#define INF_INT_MAX 2147483647
#define INF_LL 9223372036854775
#define INF_LL_MAX 9223372036854775807
#define EPS 1e-10
#define MOD 1000000007
#define MOD9 998244353
#define Pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define LD long double

#define int long long

using II = pair<int, int>;

int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }
int extgcd(int a, int b, int &x, int &y) {
  int g = a;
  x = 1;
  y = 0;
  if (b != 0)
    g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())

const int MAX_N = 1005;
const int MAX_M = 2005;

int N, M;
vector<int> Edge[MAX_N];

int used[MAX_N][2];
vector<int> ans;

int dfs(int pos, int depth = 1) {
  if (used[pos][1]) {
    return -1;
  }
  if (used[pos][0]) {
    int index = used[pos][0] - 1;
    return pos;
  }

  used[pos][0] = depth;
  ans.push_back(pos);
  for (int next : Edge[pos]) {
    int ret = dfs(next, depth + 1);
    if (ret != -1) {
      return ret;
    }
  }
  used[pos][0] = false;
  ans.pop_back();
  used[pos][1] = true;

  return -1;
}

bool edgeFlag[MAX_N][MAX_N];
bool used3[MAX_N];
int dist[MAX_N];
int rireki[MAX_N];

signed main() {
  cin >> N >> M;
  REP(m, M) {
    int a, b;
    cin >> a >> b;
    Edge[a].push_back(b);
    edgeFlag[a][b] = true;
  }

  REP(n, N + 1) {
    int ret = dfs(n);
    if (ret == -1)
      continue;
    else {
      int k = 0;
      REP(n, ans.size()) {
        if (ans[n] == ret) {
          k = n;
          break;
        }
      }

      FOR(n, k, ans.size()) { used3[ans[n]] = true; }

      vector<int> ans2;
      int dd = INF_LL;

      FOR(n, k, ans.size()) {
        memset(dist, -1, sizeof(dist));
        memset(rireki, -1, sizeof(rireki));
        queue<int> que;
        dist[ans[n]] = 0;
        que.push(ans[n]);
        int pre = -1;
        while (que.size() && pre == -1) {
          int pos = que.front();
          que.pop();
          for (int next : Edge[pos]) {
            if (used3[next] && dist[next] == -1) {
              dist[next] = dist[pos] + 1;
              que.push(next);
              rireki[next] = pos;
            }
            if (next == ans[n]) {
              pre = pos;
              break;
            }
          }
        }

        if (dd > dist[pre]) {
          dd = dist[pre];
          ans2.clear();
          ans2.push_back(pre);
          while (pre != ans[n]) {
            pre = rireki[pre];
            ans2.push_back(pre);
          }
        }
      }

      cout << ans2.size() << endl;
      for (int a : ans2) {
        cout << a << endl;
      }

      return 0;
    }
  }
  cout << -1 << endl;

  return 0;
}