Problem p03091 (C++)

Buggy submission (status: Time Limit Exceeded):
// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
using int64 = long long int;

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

void drop(string s) {
  printf("%s\n", s.c_str());
  exit(0);
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> deg(N);
  vector<vector<int>> G(N);
  for (int i = 0; i < M; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].emplace_back(v);
    G[v].emplace_back(u);
    deg[u]++, deg[v]++;
  }

  // 奇数があるなら NG
  int deg6 = 0, deg4 = 0, deg2 = 0;
  for (int i = 0; i < N; i++) {
    if (deg[i] % 2)
      drop("No");
    else if (deg[i] == 2)
      deg2++;
    else if (deg[i] == 4)
      deg4++;
    else
      deg6++;
  }
  fprintf(stderr, "# %d %d %d\n", deg6, deg4, deg2);

  // 次数 >= 6 なら 3 つ以上に分割可能
  // 次数 == 4 が 1 個以下なら無理
  // 次数 == 4 が 3 個以上なら OK
  if (deg6 >= 1 or deg4 >= 3)
    drop("Yes");
  if (deg4 <= 1)
    drop("No");

  // 次数 == 4 が 2 個の場合のみ
  {
    int s = -1, t = -1;
    for (int i = 0; i < N; i++) {
      if (deg[i] == 4) {
        if (s < 0)
          s = i;
        else
          t = i;
      }
    }

    int cnt = 0;
    for (auto v : G[s]) {
      int cur = v, pre = s;
      while (cur != s and cur != t) {
        for (auto to : G[cur]) {
          if (to == pre)
            continue;
          pre = cur;
          cur = to;
        }
      }
      if (cur == t)
        cnt++;
    }

    printf("%s\n", cnt == 4 ? "No" : "Yes");
  }
  return 0;
}


Fixed submission:
// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
using int64 = long long int;

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

void drop(string s) {
  printf("%s\n", s.c_str());
  exit(0);
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> deg(N);
  vector<vector<int>> G(N);
  for (int i = 0; i < M; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].emplace_back(v);
    G[v].emplace_back(u);
    deg[u]++, deg[v]++;
  }

  // 奇数があるなら NG
  int deg6 = 0, deg4 = 0, deg2 = 0;
  for (int i = 0; i < N; i++) {
    if (deg[i] % 2)
      drop("No");
    else if (deg[i] == 2)
      deg2++;
    else if (deg[i] == 4)
      deg4++;
    else
      deg6++;
  }
  fprintf(stderr, "# %d %d %d\n", deg6, deg4, deg2);

  // 次数 >= 6 なら 3 つ以上に分割可能
  // 次数 == 4 が 1 個以下なら無理
  // 次数 == 4 が 3 個以上なら OK
  if (deg6 >= 1 or deg4 >= 3)
    drop("Yes");
  if (deg4 <= 1)
    drop("No");

  // 次数 == 4 が 2 個の場合のみ
  {
    int s = -1, t = -1;
    for (int i = 0; i < N; i++) {
      if (deg[i] == 4) {
        if (s < 0)
          s = i;
        else
          t = i;
      }
    }

    int cnt = 0;
    for (auto v : G[s]) {
      int cur = v, pre = s;
      while (cur != s and cur != t) {
        for (auto to : G[cur]) {
          if (to == pre)
            continue;
          pre = cur;
          cur = to;
          break;
        }
      }
      if (cur == t)
        cnt++;
    }

    printf("%s\n", cnt == 4 ? "No" : "Yes");
  }
  return 0;
}