Problem p03091 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int n, m;
  cin >> n >> m;
  vector<vector<int>> edge(n);
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    edge[a].emplace_back(b);
    edge[b].emplace_back(a);
  }
  vector<int> four;
  bool six = false;
  REP(i, n) {
    if (edge[i].size() & 1) {
      cout << "No\n";
      return 0;
    }
    if (edge[i].size() == 4)
      four.emplace_back(i);
    else if (edge[i].size() >= 6)
      six = true;
  }
  if (four.size() >= 3 || six) {
    cout << "Yes\n";
    return 0;
  }
  function<void(int, int)> dfs = [&](int par, int ver) {
    for (int e : edge[ver])
      if (e != par) {
        if (e == four.front()) {
          cout << "Yes\n";
          exit(0);
        }
        if (e != four.back())
          dfs(ver, e);
      }
  };
  dfs(-1, four.front());
  cout << "No\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int n, m;
  cin >> n >> m;
  vector<vector<int>> edge(n);
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    edge[a].emplace_back(b);
    edge[b].emplace_back(a);
  }
  vector<int> four;
  bool six = false;
  REP(i, n) {
    if (edge[i].size() & 1) {
      cout << "No\n";
      return 0;
    }
    if (edge[i].size() == 4)
      four.emplace_back(i);
    else if (edge[i].size() >= 6)
      six = true;
  }
  if (four.size() >= 3 || six) {
    cout << "Yes\n";
    return 0;
  }
  if (four.size() <= 1) {
    cout << "No\n";
    return 0;
  }
  function<void(int, int)> dfs = [&](int par, int ver) {
    for (int e : edge[ver])
      if (e != par) {
        if (e == four.front()) {
          cout << "Yes\n";
          exit(0);
        }
        if (e != four.back())
          dfs(ver, e);
      }
  };
  dfs(-1, four.front());
  cout << "No\n";
  return 0;
}