Problem p02902 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i, s, e) for (i64(i) = (s); (i) < (e); (i)++)
#define all(x) x.begin(), x.end()
#define let auto const

template <typename... Types> struct dynarr : std::vector<Types...> {
  using std::vector<Types...>::vector;
  using size_type = typename std::vector<Types...>::size_type;
  auto &&operator[](size_type i) { return this->at(i); }
  auto &&operator[](size_type i) const { return this->at(i); }
};

vector<vector<i64>> aG;
vector<vector<i64>> G;
vector<vector<i64>> rG;

vector<i64> res;
vector<i64> cnt;
vector<i64> ok;

i64 AA = 0;

bool dfs(i64 v, i64 f) {
  // cout << v << " " << f << endl;
  for (i64 x : aG[v]) {
    cnt[x]++;
  }
  for (i64 x : G[v]) {
    cnt[x]--;
    if (cnt[x] == 1 && ok[x] == 1) {
      res.push_back(x);
      res.push_back(v);
      return true;
    }
    if (cnt[x] == 0 && dfs(x, v)) {
      res.push_back(v);
      return true;
    }
    cnt[x]++;
  }
  for (i64 x : aG[v]) {
    cnt[x]--;
  }
  return false;
}

int main() {
  i64 N, M;
  cin >> N >> M;
  aG.resize(N);
  G.resize(N);
  rG.resize(N);
  cnt.resize(N);
  rep(i, 0, M) {
    i64 a, b;
    cin >> a >> b;
    a--;
    b--;
    aG[a].push_back(b);
    aG[b].push_back(a);
    G[a].push_back(b);
    rG[b].push_back(a);
  }
  rep(i, 0, N) {
    cnt.assign(N, 0);
    ok.assign(N, 0);
    for (i64 x : rG[i])
      ok[x] = 1;
    if (dfs(i, -1)) {
      cout << res.size() << endl;
      for (auto x : res) {
        cout << x + 1 << endl;
      }
      return 0;
    }
  }
  cout << -1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i, s, e) for (i64(i) = (s); (i) < (e); (i)++)
#define all(x) x.begin(), x.end()
#define let auto const

template <typename... Types> struct dynarr : std::vector<Types...> {
  using std::vector<Types...>::vector;
  using size_type = typename std::vector<Types...>::size_type;
  auto &&operator[](size_type i) { return this->at(i); }
  auto &&operator[](size_type i) const { return this->at(i); }
};

vector<vector<i64>> aG;
vector<vector<i64>> G;
vector<vector<i64>> rG;

vector<i64> res;
vector<i64> cnt;
vector<i64> ok;

i64 AA = 0;

bool dfs(i64 v, i64 f) {
  // cout << v << " " << f << endl;
  for (i64 x : aG[v]) {
    cnt[x]++;
  }
  for (i64 x : G[v]) {
    cnt[x]--;
    if (cnt[x] == 1 && ok[x] == 1) {
      res.push_back(x);
      res.push_back(v);
      return true;
    }
    if (cnt[x] == 0 && dfs(x, v)) {
      res.push_back(v);
      return true;
    }
    cnt[x]++;
  }
  for (i64 x : aG[v]) {
    cnt[x]--;
  }
  cnt[v] = 1e9;
  return false;
}

int main() {
  i64 N, M;
  cin >> N >> M;
  aG.resize(N);
  G.resize(N);
  rG.resize(N);
  cnt.resize(N);
  rep(i, 0, M) {
    i64 a, b;
    cin >> a >> b;
    a--;
    b--;
    aG[a].push_back(b);
    aG[b].push_back(a);
    G[a].push_back(b);
    rG[b].push_back(a);
  }
  rep(i, 0, N) {
    cnt.assign(N, 0);
    ok.assign(N, 0);
    for (i64 x : rG[i])
      ok[x] = 1;
    if (dfs(i, -1)) {
      cout << res.size() << endl;
      for (auto x : res) {
        cout << x + 1 << endl;
      }
      return 0;
    }
  }
  cout << -1 << endl;
}