Problem p03091 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

int N, M;
vector<int> g[100010];

int countCycles() {
  vector<int> deg(N, 0);
  rep(i, N) deg[i] = g[i].size();

  int res = 0;
  map<pair<int, int>, int> mp;
  vector<int> used(N, 0);
  vector<int> pos(N, 0);
  for (int i = 0; i < N; i++) {
    if (deg[i] == 0)
      continue;

    stack<int> st;
    st.push(i);
    used[i] = 1;
    while (1) {
      int now = st.top();
      int nex = -1;
      for (; pos[now] < g[now].size(); pos[now]++) {
        int tar = g[now][pos[now]];
        if (mp.count(mkp(now, tar)))
          continue;

        nex = tar;
        pos[now]++;
        break;
      }

      if (nex == -1)
        break;
      if (used[nex]) {
        deg[now]--;
        deg[nex]--;
        mp[mkp(now, nex)] = 1;
        mp[mkp(nex, now)] = 1;
        while (st.top() != nex) {
          int tmp = st.top();
          st.pop();
          used[tmp] = 0;
        }
        res++;
        continue;
      } else {
        st.push(nex);
        deg[now]--;
        deg[nex]--;
        used[nex] = 1;
        mp[mkp(now, nex)] = 1;
        mp[mkp(nex, now)] = 1;
      }
    }
    used[i] = 0;
  }
  return res;
}

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

  cin >> N >> M;
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  rep(i, N) {
    if (g[i].size() % 2) {
      cout << "No" << endl;
      return 0;
    }
  }

  for (int k = 0; k < 50; k++) {
    random_device rg;
    mt19937 engine(rg());
    rep(i, N) shuffle(g[i].begin(), g[i].end(), engine);
    int cycles = countCycles();
    if (cycles >= 3) {
      cout << "Yes" << endl;
      return 0;
    }
  }

  cout << "No" << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

int N, M;
vector<int> g[100010];

int countCycles() {
  vector<int> deg(N, 0);
  rep(i, N) deg[i] = g[i].size();

  int res = 0;
  map<pair<int, int>, int> mp;
  vector<int> used(N, 0);
  vector<int> pos(N, 0);
  for (int i = 0; i < N; i++) {
    if (deg[i] == 0)
      continue;

    stack<int> st;
    st.push(i);
    used[i] = 1;
    while (1) {
      int now = st.top();
      int nex = -1;
      for (; pos[now] < g[now].size(); pos[now]++) {
        int tar = g[now][pos[now]];
        if (mp.count(mkp(now, tar)))
          continue;

        nex = tar;
        pos[now]++;
        break;
      }

      if (nex == -1)
        break;
      if (used[nex]) {
        deg[now]--;
        deg[nex]--;
        mp[mkp(now, nex)] = 1;
        mp[mkp(nex, now)] = 1;
        while (st.top() != nex) {
          int tmp = st.top();
          st.pop();
          used[tmp] = 0;
        }
        res++;
        continue;
      } else {
        st.push(nex);
        deg[now]--;
        deg[nex]--;
        used[nex] = 1;
        mp[mkp(now, nex)] = 1;
        mp[mkp(nex, now)] = 1;
      }
    }
    used[i] = 0;
  }
  return res;
}

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

  cin >> N >> M;
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  rep(i, N) {
    if (g[i].size() % 2) {
      cout << "No" << endl;
      return 0;
    }
  }

  for (int k = 0; k < 10; k++) {
    random_device rg;
    mt19937 engine(rg());
    rep(i, N) shuffle(g[i].begin(), g[i].end(), engine);
    int cycles = countCycles();
    if (cycles >= 3) {
      cout << "Yes" << endl;
      return 0;
    }
  }

  cout << "No" << endl;

  return 0;
}