Problem p03091 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <typename T> void vprint(T &v) {
  REP(i, v.size()) { cout << v[i] << " "; }
  cout << endl;
}

vector<ll> G[101010];

void answer(bool flag) {
  cout << (flag ? "Yes" : "No") << endl;
  exit(0);
}

vector<bool> visited(101010, false);

bool dfs(ll now, ll par, ll x, ll y) {
  if (now == y)
    return false;
  if (now == x && visited[now])
    return true;
  if (visited[now])
    return false;
  visited[now] = true;
  bool res = false;
  REP(i, G[now].size()) {
    ll next = G[now][i];
    if (next == par)
      continue;
    res = (res || dfs(next, now, x, y));
  }
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N, M;
  cin >> N >> M;
  REP(i, M) {
    ll a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  bool flag = true;
  REP(i, N) {
    if (G[i].size() % 2 != 0)
      flag = false;
  }
  if (!flag) {
    answer(false);
  }

  vector<ll> four;
  REP(i, N) {
    if (G[i].size() > 5)
      answer(true);
    if (G[i].size() == 4)
      four.push_back(i);
  }

  if (four.size() > 2)
    answer(true);
  if (four.size() == 1)
    answer(false);

  ll x = four[0];
  ll y = four[1];
  answer(dfs(x, -1, x, y));

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <typename T> void vprint(T &v) {
  REP(i, v.size()) { cout << v[i] << " "; }
  cout << endl;
}

vector<ll> G[101010];

void answer(bool flag) {
  cout << (flag ? "Yes" : "No") << endl;
  exit(0);
}

vector<bool> visited(101010, false);

bool dfs(ll now, ll par, ll x, ll y) {
  if (now == y)
    return false;
  if (now == x && visited[now])
    return true;
  if (visited[now])
    return false;
  visited[now] = true;
  bool res = false;
  REP(i, G[now].size()) {
    ll next = G[now][i];
    if (next == par)
      continue;
    res = (res || dfs(next, now, x, y));
  }
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N, M;
  cin >> N >> M;
  REP(i, M) {
    ll a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  bool flag = true;
  REP(i, N) {
    if (G[i].size() % 2 != 0)
      flag = false;
  }
  if (!flag) {
    answer(false);
  }

  vector<ll> four;
  REP(i, N) {
    if (G[i].size() > 5)
      answer(true);
    if (G[i].size() == 4)
      four.push_back(i);
  }

  if (four.size() > 2)
    answer(true);
  if (four.size() < 2)
    answer(false);

  ll x = four[0];
  ll y = four[1];
  answer(dfs(x, -1, x, y));

  return 0;
}