Problem p02902 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;
int N, M;
vector<int> pathes[1005];
bool visiting[1005];
bool visited[1005];
int indexes[1005];
bool appeared[1005];
vector<int> circuit;
vector<int> ans;
int endpoint = -1;
bool dfs(int now) {
  if (endpoint != -1)
    return false;
  visiting[now] = true;
  visited[now] = true;
  for (auto to : pathes[now]) {
    if (visiting[to]) {
      circuit.push_back(now);
      endpoint = to;
      visiting[now] = false;
      return true;
    }
    if (visited[to])
      continue;
    if (dfs(to)) {
      circuit.push_back(now);
      visiting[now] = false;
      if (endpoint == now)
        return false;
      return true;
      // return true;
    }
  }
  visiting[now] = false;
  return false;
}

int main() {
  // cout.precision(10);
  cin >> N >> M;
  for (int i = 1; i <= M; i++) {
    int a, b;
    cin >> a >> b;
    pathes[a].push_back(b);
  }
  for (int i = 1; i <= N; i++) {
    if (visited[i])
      continue;
    if (dfs(i))
      break;
  }
  if (circuit.empty()) {
    cout << -1 << endl;
    return 0;
  }
  reverse(circuit.begin(), circuit.end());
  for (auto v : circuit)
    cerr << v << " ";
  cerr << endl;
  bool ok = false;
  int n = circuit.size() - 1;
  for (auto to : pathes[circuit[n]]) {
    if (to == circuit[0])
      ok = true;
  }
  if (!ok) {
    cerr << "NG" << endl;
    for (int i = 1; i <= 1e8; i++)
      cout << i << endl;
  }
  for (int i = 1; i <= N; i++)
    indexes[i] = -1;
  for (int i = 0; i < circuit.size(); i++) {
    indexes[circuit[i]] = i;
  }
  for (int i = 0; i < circuit.size(); i++) {
    appeared[circuit[i]] = true;
    int maxi = -1;
    for (auto to : pathes[circuit[i]]) {
      if (indexes[to] == -1)
        continue;
      if (!appeared[to])
        continue;
      if (indexes[to] < i)
        chmax(maxi, indexes[to]);
    }
    if (maxi != -1) {
      cerr << "B" << endl;
      for (int j = maxi; j <= i; j++)
        if (appeared[circuit[j]])
          ans.push_back(circuit[j]);
      cout << ans.size() << endl;
      for (auto to : ans)
        cout << to << endl;
      return 0;
    }
    if (i + 1 == circuit.size()) {
      cerr << "A" << endl;
      for (int j = 0; j < circuit.size(); j++)
        if (appeared[circuit[j]])
          ans.push_back(circuit[j]);
      cout << ans.size() << endl;
      for (auto to : ans)
        cout << to << endl;
      return 0;
    }
    maxi = -1;
    for (auto to : pathes[circuit[i]]) {
      if (indexes[to] == -1)
        continue;
      if (indexes[to] > i)
        chmax(maxi, indexes[to]);
    }
    if (maxi != -1)
      i = maxi - 1;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;
int N, M;
vector<int> pathes[1005];
bool visiting[1005];
bool visited[1005];
int indexes[1005];
bool appeared[1005];
vector<int> circuit;
vector<int> ans;
int endpoint = -1;
bool dfs(int now) {
  if (endpoint != -1)
    return false;
  visiting[now] = true;
  visited[now] = true;
  for (auto to : pathes[now]) {
    if (endpoint != -1)
      break;
    if (visiting[to]) {
      circuit.push_back(now);
      endpoint = to;
      visiting[now] = false;
      return true;
    }
    if (visited[to])
      continue;
    if (dfs(to)) {
      circuit.push_back(now);
      visiting[now] = false;
      if (endpoint == now)
        return false;
      return true;
      // return true;
    }
  }
  visiting[now] = false;
  return false;
}

int main() {
  // cout.precision(10);
  cin >> N >> M;
  for (int i = 1; i <= M; i++) {
    int a, b;
    cin >> a >> b;
    pathes[a].push_back(b);
  }
  for (int i = 1; i <= N; i++) {
    if (visited[i])
      continue;
    if (dfs(i))
      break;
  }
  if (circuit.empty()) {
    cout << -1 << endl;
    return 0;
  }
  reverse(circuit.begin(), circuit.end());
  for (auto v : circuit)
    cerr << v << " ";
  cerr << endl;
  bool ok = false;
  int n = circuit.size() - 1;
  for (auto to : pathes[circuit[n]]) {
    if (to == circuit[0])
      ok = true;
  }
  if (!ok) {
    cerr << "NG" << endl;
    for (int i = 1; i <= 1e8; i++)
      cout << i << endl;
  }
  for (int i = 1; i <= N; i++)
    indexes[i] = -1;
  for (int i = 0; i < circuit.size(); i++) {
    indexes[circuit[i]] = i;
  }
  for (int i = 0; i < circuit.size(); i++) {
    appeared[circuit[i]] = true;
    int maxi = -1;
    for (auto to : pathes[circuit[i]]) {
      if (indexes[to] == -1)
        continue;
      if (!appeared[to])
        continue;
      if (indexes[to] < i)
        chmax(maxi, indexes[to]);
    }
    if (maxi != -1) {
      cerr << "B" << endl;
      for (int j = maxi; j <= i; j++)
        if (appeared[circuit[j]])
          ans.push_back(circuit[j]);
      cout << ans.size() << endl;
      for (auto to : ans)
        cout << to << endl;
      return 0;
    }
    if (i + 1 == circuit.size()) {
      cerr << "A" << endl;
      for (int j = 0; j < circuit.size(); j++)
        if (appeared[circuit[j]])
          ans.push_back(circuit[j]);
      cout << ans.size() << endl;
      for (auto to : ans)
        cout << to << endl;
      return 0;
    }
    maxi = -1;
    for (auto to : pathes[circuit[i]]) {
      if (indexes[to] == -1)
        continue;
      if (indexes[to] > i)
        chmax(maxi, indexes[to]);
    }
    if (maxi != -1)
      i = maxi - 1;
  }
  return 0;
}