Problem p02902 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
using P = pair<int, int>;

vector<vector<int>> edge;

stack<int> hist;
vector<bool> seen;

bool dfs(int p, int t) {
  for (auto nt : edge[t]) {
    if (nt == p) {
      return true;
    } else if (seen[nt])
      continue;

    hist.push(nt);
    seen[nt] = true;
    bool ans = dfs(p, nt);

    if (ans)
      return true;
    hist.pop();
  }
  return false;
}

int main() {
  int n, m;
  cin >> n >> m;
  edge.resize(n + 1);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    edge[a].push_back(b);
  }

  vector<int> cp;
  for (int p = 1; p <= n; p++) {
    seen.assign(n + 1, false);
    while (!hist.empty())
      hist.pop();
    hist.push(p);
    seen[p] = true;
    bool ok = dfs(p, p);
    if (ok) {
      while (!hist.empty()) {
        cp.push_back(hist.top());
        hist.pop();
      }
      reverse(cp.begin(), cp.end());
      break;
    }
  }
  if (cp.empty()) {
    cout << -1 << endl;
    return 0;
  }
  vector<int> path(n + 1, -1);
  for (int i = 0; i < cp.size(); i++) {
    path[cp[i]] = i;
  }
  while (true) {
    bool cut = false;
    for (int i = 0; i < cp.size(); i++) {
      for (auto nt : edge[cp[i]]) {
        if (path[nt] != (i + 1) % cp.size() && path[nt] > 0) {
          cut = true;
          int from = i;
          int to = path[nt];
          vector<int> ncp;
          if (from < to) {
            for (int j = to; j < cp.size(); j++) {
              ncp.push_back(cp[j]);
            }
            for (int j = 0; j <= from; j++) {
              ncp.push_back(cp[j]);
            }
          } else {
            for (int j = to; j <= from; j++) {
              ncp.push_back(cp[j]);
            }
          }
          cp = ncp;
          break;
        }
      }
      if (cut)
        break;
    }
    if (!cut)
      break;
  }

  cout << cp.size() << endl;
  for (auto p : cp)
    cout << p << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
using P = pair<int, int>;

vector<vector<int>> edge;

stack<int> hist;
vector<bool> seen;

bool dfs(int p, int t) {
  for (auto nt : edge[t]) {
    if (nt == p) {
      return true;
    } else if (seen[nt])
      continue;

    hist.push(nt);
    seen[nt] = true;
    bool ans = dfs(p, nt);

    if (ans)
      return true;
    hist.pop();
  }
  return false;
}

int main() {
  int n, m;
  cin >> n >> m;
  edge.resize(n + 1);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    edge[a].push_back(b);
  }

  vector<int> cp;
  for (int p = 1; p <= n; p++) {
    seen.assign(n + 1, false);
    while (!hist.empty())
      hist.pop();
    hist.push(p);
    seen[p] = true;
    bool ok = dfs(p, p);
    if (ok) {
      while (!hist.empty()) {
        cp.push_back(hist.top());
        hist.pop();
      }
      reverse(cp.begin(), cp.end());
      break;
    }
  }
  if (cp.empty()) {
    cout << -1 << endl;
    return 0;
  }
  vector<int> path(n + 1, -1);
  for (int i = 0; i < cp.size(); i++) {
    path[cp[i]] = i;
  }
  while (true) {
    bool cut = false;
    path.assign(n + 1, -1);
    for (int i = 0; i < cp.size(); i++) {
      path[cp[i]] = i;
    }
    for (int i = 0; i < cp.size(); i++) {
      for (auto nt : edge[cp[i]]) {
        if (path[nt] != (i + 1) % cp.size() && path[nt] > 0) {
          cut = true;
          int from = i;
          int to = path[nt];
          vector<int> ncp;
          if (from < to) {
            for (int j = to; j < cp.size(); j++) {
              ncp.push_back(cp[j]);
            }
            for (int j = 0; j <= from; j++) {
              ncp.push_back(cp[j]);
            }
          } else {
            for (int j = to; j <= from; j++) {
              ncp.push_back(cp[j]);
            }
          }
          cp = ncp;
          break;
        }
      }
      if (cut)
        break;
    }
    if (!cut)
      break;
  }

  cout << cp.size() << endl;
  for (auto p : cp)
    cout << p << endl;
  return 0;
}