Problem p03746 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES

#include <bits/stdc++.h>
using namespace std;
using i32 = int;
using i64 = long long int;
using f64 = double;
using str = string;
template <typename T> using vec = vector<T>;
template <typename T> using heap = priority_queue<T, vec<T>, greater<T>>;
#define times(n, i) for (i32 i = 0; i < (n); ++i)
#define range(a, b, i) for (i32 i = (a); i < (b); ++i)
#define upto(a, b, i) for (i32 i = (a); i <= (b); ++i)
#define downto(a, b, i) for (i32 i = (a); i >= (b); --i)
#define all(xs) (xs).begin(), (xs).end()
#define sortall(xs) sort(all(xs))
#define reverseall(xs) reverse(all(xs))
#define uniqueall(xs) (xs).erase(unique(all(xs)), (xs).end())
#define even(x) (((x)&1) == 0)
#define odd(x) (((x)&1) == 1)
#define append emplace_back
#define findge lower_bound
#define findgt upper_bound
const i64 MOD = 1000000007;
const f64 EPS = 1e-10;

i64 n, m;
set<i32> v[100001];
vec<i32> ans1, ans2;
set<i32> s;

bool valid() {
  return v[*ans1.rbegin()].size() == 0 && v[*ans2.rbegin()].size() == 0;
}

i32 main() {
  cin >> n >> m;
  times(m, i) {
    i32 a, b;
    cin >> a >> b;
    v[a].insert(b);
    v[b].insert(a);
  }

  i32 a = 1, b = *v[a].begin();
  ans1.append(a);
  ans2.append(b);
  for (i32 i : v[a]) {
    v[i].erase(a);
  }
  for (i32 i : v[b]) {
    v[i].erase(b);
  }
  s.insert(a);
  s.insert(b);
  while (!valid()) {
    a = *ans1.rbegin();
    b = *ans2.rbegin();
    while (v[a].size() > 0 && s.find(*v[a].begin()) != s.end()) {
      v[a].erase(v[a].begin());
    }
    while (v[b].size() > 0 && s.find(*v[b].begin()) != s.end()) {
      v[b].erase(v[b].begin());
    }
    bool added = false;
    i32 i;
    if (v[b].size() > 0) {
      i = *v[b].begin();
      ans2.append(i);
      added = true;
    }
    if (!added && v[a].size() > 0) {
      i = *v[a].begin();
      ans1.append(i);
    }
    if (!added)
      continue;
    for (i32 j : v[i]) {
      v[j].erase(i);
    }
    s.insert(i);
  }
  vec<i32> ans;
  downto(ans1.size() - 1, 0, i) { ans.append(ans1[i]); }
  upto(0, ans2.size() - 1, i) { ans.append(ans2[i]); }
  cout << ans.size() << endl;
  times(ans.size() - 1, i) { cout << ans[i] << " "; }
  cout << ans[ans.size() - 1] << endl;
  return 0;
}

Fixed submission:
#define _USE_MATH_DEFINES

#include <bits/stdc++.h>
using namespace std;
using i32 = int;
using i64 = long long int;
using f64 = double;
using str = string;
template <typename T> using vec = vector<T>;
template <typename T> using heap = priority_queue<T, vec<T>, greater<T>>;
#define times(n, i) for (i32 i = 0; i < (n); ++i)
#define range(a, b, i) for (i32 i = (a); i < (b); ++i)
#define upto(a, b, i) for (i32 i = (a); i <= (b); ++i)
#define downto(a, b, i) for (i32 i = (a); i >= (b); --i)
#define all(xs) (xs).begin(), (xs).end()
#define sortall(xs) sort(all(xs))
#define reverseall(xs) reverse(all(xs))
#define uniqueall(xs) (xs).erase(unique(all(xs)), (xs).end())
#define even(x) (((x)&1) == 0)
#define odd(x) (((x)&1) == 1)
#define append emplace_back
#define findge lower_bound
#define findgt upper_bound
const i64 MOD = 1000000007;
const f64 EPS = 1e-10;

i64 n, m;
set<i32> v[100001];
vec<i32> ans1, ans2;
set<i32> s;

bool valid() {
  return v[*ans1.rbegin()].size() == 0 && v[*ans2.rbegin()].size() == 0;
}

i32 main() {
  cin >> n >> m;
  times(m, i) {
    i32 a, b;
    cin >> a >> b;
    v[a].insert(b);
    v[b].insert(a);
  }

  i32 a = 1, b = *v[a].begin();
  ans1.append(a);
  ans2.append(b);
  for (i32 i : v[a]) {
    v[i].erase(a);
  }
  for (i32 i : v[b]) {
    v[i].erase(b);
  }
  s.insert(a);
  s.insert(b);
  while (!valid()) {
    a = *ans1.rbegin();
    b = *ans2.rbegin();
    while (v[a].size() > 0 && s.find(*v[a].begin()) != s.end()) {
      v[a].erase(v[a].begin());
    }
    while (v[b].size() > 0 && s.find(*v[b].begin()) != s.end()) {
      v[b].erase(v[b].begin());
    }
    bool added = false;
    i32 i;
    if (v[b].size() > 0) {
      i = *v[b].begin();
      ans2.append(i);
      added = true;
    }
    if (!added && v[a].size() > 0) {
      i = *v[a].begin();
      ans1.append(i);
      added = true;
    }
    if (!added)
      continue;
    for (i32 j : v[i]) {
      v[j].erase(i);
    }
    s.insert(i);
  }
  vec<i32> ans;
  downto(ans1.size() - 1, 0, i) { ans.append(ans1[i]); }
  upto(0, ans2.size() - 1, i) { ans.append(ans2[i]); }
  cout << ans.size() << endl;
  times(ans.size() - 1, i) { cout << ans[i] << " "; }
  cout << ans[ans.size() - 1] << endl;
  return 0;
}