Problem p03746 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <ciso646>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)

#define inf 0x3f3f3f3f
#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#define SET(a, c) memset(a, c, sizeof a)
#define CLR(a) memset(a, 0, sizeof a)
#define VS vector<string>
#define VI vector<ll>
#define DEBUG(x) cout << #x << ": " << x << endl
#define MIN(a, b) (a > b ? b : a)
#define MAX(a, b) (a > b ? a : b)
#define pi 2 * acos(0.0)
#define INFILE() freopen("in0.txt", "r", stdin)
#define OUTFILE() freopen("out0.txt", "w", stdout)
#define ll long long
#define ull unsigned long long
#define pii pair<ll, ll>
#define pcc pair<char, char>
#define pic pair<ll, char>
#define pci pair<char, ll>
#define eps 1e-14
#define FST first
#define SEC second
#define SETUP cin.tie(0), ios::sync_with_stdio(false), cout << setprecision(15)

namespace {
struct input_returnner {
  ll N;
  input_returnner(ll N_ = 0) : N(N_) {}
  template <typename T> operator vector<T>() const {
    vector<T> res(N);
    for (auto &a : res)
      cin >> a;
    return std::move(res);
  }
  template <typename T> operator T() const {
    T res;
    cin >> res;
    return res;
  }
  template <typename T> T operator-(T right) {
    return T(input_returnner()) - right;
  }
  template <typename T> T operator+(T right) {
    return T(input_returnner()) + right;
  }
  template <typename T> T operator*(T right) {
    return T(input_returnner()) * right;
  }
  template <typename T> T operator/(T right) {
    return T(input_returnner()) / right;
  }
  template <typename T> T operator<<(T right) {
    return T(input_returnner()) << right;
  }
  template <typename T> T operator>>(T right) {
    return T(input_returnner()) >> right;
  }
};
template <typename T> input_returnner in() { return in<T>(); }
input_returnner in() { return input_returnner(); }
input_returnner in(ll N) { return std::move(input_returnner(N)); }
} // namespace

const ll MOD = 1e9 + 7;

struct ModInt {
  ll v = 0;
  ModInt() {}
  template <class T> ModInt(const T &right) {
    v = right;
    if (v >= 0)
      v %= MOD;
    else
      v += ((-v) / MOD + 1) * MOD;
    v %= MOD;
  }
  void operator=(const ModInt &right) { v = right.v; }
  template <class T> void operator=(const T &right) {
    v = right;
    if (v >= 0)
      v %= MOD;
    else
      v = v += ((-v) / MOD + 1) * MOD;
    v %= MOD;
  }

  ModInt operator+(const ModInt &right) { return (v + right.v) % MOD; }
  ModInt operator-(const ModInt &right) { return (MOD - (v - right.v)); }
  ModInt operator*(const ModInt &right) { return (v * right.v) % MOD; }
  ModInt operator/(const ModInt &right) { return v / right.v; }

  void operator+=(const ModInt &right) { v = (v + right.v) % MOD; }
  void operator-=(const ModInt &right) { v = (MOD - (v - right.v)); }
  void operator*=(const ModInt &right) { v = (v * right.v) % MOD; }
  void operator/=(const ModInt &right) { v = v / right.v; }

  bool operator==(const ModInt &right) { return v == right.v; }
};

ostream &operator<<(ostream &os, const ModInt &value) {
  os << value.v;
  return os;
}

void solve();
/// ---template---

signed main(void) {
  SETUP;
  solve();
  return 0;
}

vector<vector<int>> G;

list<int> DFS(int v, vector<bool> visited) {
  visited[v] = true;
  bool isVisit = false;
  list<int> res;
  for (auto &t : G[v]) {
    if (not visited[t]) {
      res.splice(res.end(), DFS(t, visited));
      isVisit = true;
      break;
    }
  }
  res.push_back(v);
  return res;
}

void solve() {
  int N, M;
  cin >> N >> M;
  vector<pii> edge(N);

  G.resize(N);

  REP(i, M) {
    int A, B;
    cin >> A >> B;
    --A;
    --B;
    G[A].push_back(B);
    G[B].push_back(A);
  }

  int start = 0;
  vector<bool> visited(N);
  list<int> path1 = DFS(start, visited);
  for (auto &a : path1)
    visited[a] = true;
  list<int> path2 = DFS(start, visited);
  path2.pop_back();
  cout << path1.size() + path2.size() << endl;
  for (auto &a : path2) {
    cout << a + 1 << " ";
  }
  for (auto it = path1.rbegin(); it != path1.rend(); ++it) {
    cout << *it + 1 << " ";
  }
  cout << endl;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <ciso646>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)

#define inf 0x3f3f3f3f
#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#define SET(a, c) memset(a, c, sizeof a)
#define CLR(a) memset(a, 0, sizeof a)
#define VS vector<string>
#define VI vector<ll>
#define DEBUG(x) cout << #x << ": " << x << endl
#define MIN(a, b) (a > b ? b : a)
#define MAX(a, b) (a > b ? a : b)
#define pi 2 * acos(0.0)
#define INFILE() freopen("in0.txt", "r", stdin)
#define OUTFILE() freopen("out0.txt", "w", stdout)
#define ll long long
#define ull unsigned long long
#define pii pair<ll, ll>
#define pcc pair<char, char>
#define pic pair<ll, char>
#define pci pair<char, ll>
#define eps 1e-14
#define FST first
#define SEC second
#define SETUP cin.tie(0), ios::sync_with_stdio(false), cout << setprecision(15)

namespace {
struct input_returnner {
  ll N;
  input_returnner(ll N_ = 0) : N(N_) {}
  template <typename T> operator vector<T>() const {
    vector<T> res(N);
    for (auto &a : res)
      cin >> a;
    return std::move(res);
  }
  template <typename T> operator T() const {
    T res;
    cin >> res;
    return res;
  }
  template <typename T> T operator-(T right) {
    return T(input_returnner()) - right;
  }
  template <typename T> T operator+(T right) {
    return T(input_returnner()) + right;
  }
  template <typename T> T operator*(T right) {
    return T(input_returnner()) * right;
  }
  template <typename T> T operator/(T right) {
    return T(input_returnner()) / right;
  }
  template <typename T> T operator<<(T right) {
    return T(input_returnner()) << right;
  }
  template <typename T> T operator>>(T right) {
    return T(input_returnner()) >> right;
  }
};
template <typename T> input_returnner in() { return in<T>(); }
input_returnner in() { return input_returnner(); }
input_returnner in(ll N) { return std::move(input_returnner(N)); }
} // namespace

const ll MOD = 1e9 + 7;

struct ModInt {
  ll v = 0;
  ModInt() {}
  template <class T> ModInt(const T &right) {
    v = right;
    if (v >= 0)
      v %= MOD;
    else
      v += ((-v) / MOD + 1) * MOD;
    v %= MOD;
  }
  void operator=(const ModInt &right) { v = right.v; }
  template <class T> void operator=(const T &right) {
    v = right;
    if (v >= 0)
      v %= MOD;
    else
      v = v += ((-v) / MOD + 1) * MOD;
    v %= MOD;
  }

  ModInt operator+(const ModInt &right) { return (v + right.v) % MOD; }
  ModInt operator-(const ModInt &right) { return (MOD - (v - right.v)); }
  ModInt operator*(const ModInt &right) { return (v * right.v) % MOD; }
  ModInt operator/(const ModInt &right) { return v / right.v; }

  void operator+=(const ModInt &right) { v = (v + right.v) % MOD; }
  void operator-=(const ModInt &right) { v = (MOD - (v - right.v)); }
  void operator*=(const ModInt &right) { v = (v * right.v) % MOD; }
  void operator/=(const ModInt &right) { v = v / right.v; }

  bool operator==(const ModInt &right) { return v == right.v; }
};

ostream &operator<<(ostream &os, const ModInt &value) {
  os << value.v;
  return os;
}

void solve();
/// ---template---

signed main(void) {
  SETUP;
  solve();
  return 0;
}

vector<vector<int>> G;

list<int> DFS(int v, vector<bool> &visited) {
  visited[v] = true;
  bool isVisit = false;
  list<int> res;
  for (auto &t : G[v]) {
    if (not visited[t]) {
      res.splice(res.end(), DFS(t, visited));
      isVisit = true;
      break;
    }
  }
  res.push_back(v);
  return res;
}

void solve() {
  int N, M;
  cin >> N >> M;
  vector<pii> edge(N);

  G.resize(N);

  REP(i, M) {
    int A, B;
    cin >> A >> B;
    --A;
    --B;
    G[A].push_back(B);
    G[B].push_back(A);
  }

  int start = 0;
  vector<bool> visited(N);
  list<int> path1 = DFS(start, visited);
  for (auto &a : path1)
    visited[a] = true;
  list<int> path2 = DFS(start, visited);
  path2.pop_back();
  cout << path1.size() + path2.size() << endl;
  for (auto &a : path2) {
    cout << a + 1 << " ";
  }
  for (auto it = path1.rbegin(); it != path1.rend(); ++it) {
    cout << *it + 1 << " ";
  }
  cout << endl;
}