Problem p02902 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unistd.h>
#include <vector>

#ifndef ONLINE_JUDGE
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#else
#define DEBUG(x)                                                               \
  do {                                                                         \
  } while (0);
#endif

#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define FOR(i, A, N) for (int(i) = (A); (i) < (N); (i)++)
#define REP(i, N) for (int(i) = 0; (i) < (N); (i)++)

using namespace std;
typedef long long ll;
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }

int n, m;

class CycleFinder {
public:
  map<int, int> state;
  map<int, vector<int>> nxt;
  vector<int> cycle;
  set<int> vertices;
  vector<int> stk;
  void dfs(int u) {
    state[u] = 1;
    stk.pb(u);
    for (int v : nxt[u]) {
      if (cycle.size() == 0 && state[v] == 1) {
        bool write = false;
        for (int j = 0; j < stk.size(); j++) {
          if (stk[j] == v) {
            write = true;
          }
          if (write)
            cycle.pb(stk[j]);
        }
      } else if (!state[v]) {
        dfs(v);
      }
    }
    stk.pop_back();
    state[u] = 2;
  }
  vector<int> find() {
    vector<int> ord;
    for (int i : vertices)
      ord.pb(i);
    random_shuffle(ord.begin(), ord.end());
    for (int i : ord)
      if (!state[i]) {
        dfs(i);
      }
    return cycle;
  }
};

int main() {
  scanf("%d%d", &n, &m);
  CycleFinder initial;
  vector<pair<int, int>> edges;
  REP(i, n) {
    initial.state[i] = 0;
    initial.vertices.insert(i);
    initial.nxt[i] = vector<int>{};
  }
  REP(i, m) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    edges.pb({a, b});
    initial.nxt[a].pb(b);
  }

  vector<int> initCycle = initial.find();
  if (initCycle.size() == 0) {
    printf("-1\n");
    return 0;
  }

  vector<int> currCycle = initCycle;
  vector<int> nxtCycle;
  for (int t = 0; t < 3000; t++) {
    CycleFinder cf;
    set<int> has;
    for (int i : currCycle) {
      has.insert(i);
      cf.state[i] = 0;
      cf.vertices.insert(i);
      cf.nxt[i] = vector<int>{};
    }
    for (auto e : edges) {
      if (has.count(e.first) && has.count(e.second)) {
        cf.nxt[e.first].pb(e.second);
      }
    }
    for (int i : currCycle) {
      random_shuffle(cf.nxt[i].begin(), cf.nxt[i].end());
    }
    currCycle = cf.find();
  }
  printf("%d\n", currCycle.size());
  for (int x : currCycle)
    printf("%d\n", x + 1);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unistd.h>
#include <vector>

#ifndef ONLINE_JUDGE
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#else
#define DEBUG(x)                                                               \
  do {                                                                         \
  } while (0);
#endif

#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define FOR(i, A, N) for (int(i) = (A); (i) < (N); (i)++)
#define REP(i, N) for (int(i) = 0; (i) < (N); (i)++)

using namespace std;
typedef long long ll;
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }

int n, m;

class CycleFinder {
public:
  map<int, int> state;
  map<int, vector<int>> nxt;
  vector<int> cycle;
  set<int> vertices;
  vector<int> stk;
  void dfs(int u) {
    state[u] = 1;
    stk.pb(u);
    for (int v : nxt[u]) {
      if (cycle.size() == 0 && state[v] == 1) {
        bool write = false;
        for (int j = 0; j < stk.size(); j++) {
          if (stk[j] == v) {
            write = true;
          }
          if (write)
            cycle.pb(stk[j]);
        }
      } else if (!state[v]) {
        dfs(v);
      }
    }
    stk.pop_back();
    state[u] = 2;
  }
  vector<int> find() {
    vector<int> ord;
    for (int i : vertices)
      ord.pb(i);
    random_shuffle(ord.begin(), ord.end());
    for (int i : ord)
      if (!state[i]) {
        dfs(i);
      }
    return cycle;
  }
};

int main() {
  scanf("%d%d", &n, &m);
  CycleFinder initial;
  vector<pair<int, int>> edges;
  REP(i, n) {
    initial.state[i] = 0;
    initial.vertices.insert(i);
    initial.nxt[i] = vector<int>{};
  }
  REP(i, m) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    edges.pb({a, b});
    initial.nxt[a].pb(b);
  }

  vector<int> initCycle = initial.find();
  if (initCycle.size() == 0) {
    printf("-1\n");
    return 0;
  }

  vector<int> currCycle = initCycle;
  vector<int> nxtCycle;
  for (int t = 0; t < 1000; t++) {
    CycleFinder cf;
    set<int> has;
    for (int i : currCycle) {
      has.insert(i);
      cf.state[i] = 0;
      cf.vertices.insert(i);
      cf.nxt[i] = vector<int>{};
    }
    for (auto e : edges) {
      if (has.count(e.first) && has.count(e.second)) {
        cf.nxt[e.first].pb(e.second);
      }
    }
    for (int i : currCycle) {
      random_shuffle(cf.nxt[i].begin(), cf.nxt[i].end());
    }
    currCycle = cf.find();
  }
  printf("%d\n", currCycle.size());
  for (int x : currCycle)
    printf("%d\n", x + 1);
  return 0;
}