Problem p03746 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
// #pragma GCC target("avx2")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

template <typename T> void print(T x) { cout << x << "\n"; }

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int n, m;
  cin >> n >> m;

  vi a(m), b(m);
  REP(i, m) cin >> a[i] >> b[i];
  REP(i, m) a[i]--, b[i]--;

  vector<vi> edge(m);
  REP(i, m) {
    edge[a[i]].eb(b[i]);
    edge[b[i]].eb(a[i]);
  }

  vector<bool> used(n, false);
  used[a[0]] = true;
  used[b[0]] = true;

  auto dfs = [&](auto &&f, int cur, vi &ans) -> void {
    for (auto &e : edge[cur]) {
      if (used[e])
        continue;

      used[e] = true;
      ans.pb(e);
      f(f, e, ans);
      return;
    }
  };

  vi ans = {a[0], b[0]};
  dfs(dfs, b[0], ans);

  reverse(ALL(ans));
  dfs(dfs, a[0], ans);

  print(ans.size());
  for (int i = 0; i < ans.size(); i++) {
    if (i > 0)
      cout << " ";
    cout << ans[i] + 1;
  }

  cout << "\n";
}

Fixed submission:
#pragma GCC optimize("O3")
// #pragma GCC target("avx2")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

template <typename T> void print(T x) { cout << x << "\n"; }

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int n, m;
  cin >> n >> m;

  vi a(m), b(m);
  REP(i, m) cin >> a[i] >> b[i];
  REP(i, m) a[i]--, b[i]--;

  vector<vi> edge(n);
  REP(i, m) {
    edge[a[i]].eb(b[i]);
    edge[b[i]].eb(a[i]);
  }

  vector<bool> used(n, false);
  used[a[0]] = true;
  used[b[0]] = true;

  auto dfs = [&](auto &&f, int cur, vi &ans) -> void {
    for (auto &e : edge[cur]) {
      if (used[e])
        continue;

      used[e] = true;
      ans.pb(e);
      f(f, e, ans);
      return;
    }
  };

  vi ans = {a[0], b[0]};
  dfs(dfs, b[0], ans);

  reverse(ALL(ans));
  dfs(dfs, a[0], ans);

  print(ans.size());
  for (int i = 0; i < ans.size(); i++) {
    if (i > 0)
      cout << " ";
    cout << ans[i] + 1;
  }

  cout << "\n";
}