Problem p03091 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_set>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define szz(x) (int)x.size()
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) x.begin(), x.end()
typedef tuple<int, int, int> t3;

int n, m;
int deg[100010];
vector<pii> E[100010];
t3 edge[100010];
int chk[100010];

void dfs(int x, int st) {
  chk[x] = 1;
  t3 e = edge[x];
  int a, b, idx;
  tie(a, b, idx) = e;
  if (st == b)
    swap(a, b);
  int f = -1;
  for (int i = 0; i < szz(E[b]); i++)
    if (E[b][i].Fi == a)
      f = i;
  f ^= 1;
  if (chk[E[b][f].Se] == 0)
    dfs(E[b][f].Se, b);
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    deg[x]++;
    deg[y]++;
    E[x].pb(pii(y, i));
    E[y].pb(pii(x, i));
    edge[i] = t3(x, y, i);
  }
  for (int i = 1; i <= n; i++)
    if (deg[i] & 1) {
      puts("No");
      return 0;
    }
  for (int i = 1; i <= n; i++)
    if (deg[i] >= 6) {
      puts("Yes");
      return 0;
    }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    if (deg[i] == 4)
      ++ans;
  if (ans >= 3)
    puts("Yes");
  else if (ans == 1)
    puts("No");
  else {
    int fa = -1, fb = -1;
    for (int i = 1; i <= n; i++)
      if (deg[i] == 4) {
        if (fa == -1)
          fa = i;
        else
          fb = i;
      }
    int ok = 0;
    rep(a, 3) {
      rep(b, 3) {
        int cc = 0;
        memset(chk, 0, sizeof chk);
        for (int i = 1; i <= m; i++)
          if (chk[i] == 0) {
            ++cc;
            dfs(i, get<0>(edge[i]));
          }
        if (cc >= 3)
          ok = 1;
        rotate(E[fa].begin() + 1, E[fa].begin() + 2, E[fa].begin() + 4);
      }
      rotate(E[fb].begin() + 1, E[fb].begin() + 2, E[fb].begin() + 4);
    }
    printf("%s\n", ok ? "Yes" : "No");
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_set>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define szz(x) (int)x.size()
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) x.begin(), x.end()
typedef tuple<int, int, int> t3;

int n, m;
int deg[100010];
vector<pii> E[100010];
t3 edge[100010];
int chk[100010];

void dfs(int x, int st) {
  chk[x] = 1;
  t3 e = edge[x];
  int a, b, idx;
  tie(a, b, idx) = e;
  if (st == b)
    swap(a, b);
  int f = -1;
  for (int i = 0; i < szz(E[b]); i++)
    if (E[b][i].Fi == a)
      f = i;
  f ^= 1;
  if (chk[E[b][f].Se] == 0)
    dfs(E[b][f].Se, b);
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    deg[x]++;
    deg[y]++;
    E[x].pb(pii(y, i));
    E[y].pb(pii(x, i));
    edge[i] = t3(x, y, i);
  }
  for (int i = 1; i <= n; i++)
    if (deg[i] & 1) {
      puts("No");
      return 0;
    }
  for (int i = 1; i <= n; i++)
    if (deg[i] >= 6) {
      puts("Yes");
      return 0;
    }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    if (deg[i] == 4)
      ++ans;
  if (ans >= 3)
    puts("Yes");
  else if (ans <= 1)
    puts("No");
  else {
    int fa = -1, fb = -1;
    for (int i = 1; i <= n; i++)
      if (deg[i] == 4) {
        if (fa == -1)
          fa = i;
        else
          fb = i;
      }
    int ok = 0;
    rep(a, 3) {
      rep(b, 3) {
        int cc = 0;
        memset(chk, 0, sizeof chk);
        for (int i = 1; i <= m; i++)
          if (chk[i] == 0) {
            ++cc;
            dfs(i, get<0>(edge[i]));
          }
        if (cc >= 3)
          ok = 1;
        rotate(E[fa].begin() + 1, E[fa].begin() + 2, E[fa].begin() + 4);
      }
      rotate(E[fb].begin() + 1, E[fb].begin() + 2, E[fb].begin() + 4);
    }
    printf("%s\n", ok ? "Yes" : "No");
  }
  return 0;
}