Problem p03091 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define X first
#define Y second
#define pb emplace_back
#define FOR(i, a, b) for (int(i) = (a); i < (b); ++(i))
#define EFOR(i, a, b) for (int(i) = (a); i <= (b); ++(i))
#define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X))
#define REP rep
#define rrep(X, Y) for (int(X) = (Y)-1; (X) >= 0; --(X))
#define all(X) (X).begin(), (X).end()
#define eb emplace_back

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef ll LL;
typedef pii PII;
typedef pll PLL;
const ll MOD = 1e9 + 7;

#define rall(X) (X).rbegin(), (X).rend()
#define UNIQUE(X) (X).erase(unique(all(X)), (X).end())
#define reps(X, S, Y) for (int(X) = S; (X) < (Y); ++(X))
#define rreps(X, S, Y) for (int(X) = (Y)-1; (X) >= (S); --(X))
template <class T> inline bool MX(T &l, const T &r) {
  return l < r ? l = r, 1 : 0;
}
template <class T> inline bool MN(T &l, const T &r) {
  return l > r ? l = r, 1 : 0;
}

int N;
int M;
set<int> es[114514];
bool used[114514];
int prv[114514];
int depth[114514];

void Erase(int u, int v) {
  rep(i, 2) {
    assert(es[u].count(v));
    es[u].erase(v);
    swap(u, v);
  }
}

bool check() {
  fill(used, used + N, false);
  fill(prv, prv + N, -1);

  queue<int> que;
  int root = -1;
  rep(v, N) {
    if (!es[v].empty()) {
      root = v;
      used[v] = 1;
      que.push(v);
      break;
    }
  }
  if (root == -1)
    return false;

  using Ans = pair<int, pii>;
  Ans ans(MOD, pii(-1, -1));
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int u : es[v]) {
      if (!used[u]) {
        prv[u] = v;
        used[u] = true;
        depth[u] = depth[v] + 1;
        que.push(u);
      } else if (prv[v] != u) {
        MN(ans, Ans(1 + depth[v] + depth[u], pii(v, u)));
      }
    }
  }
  if (ans.X == MOD)
    return false;

  int p, q;
  tie(p, q) = ans.Y;
  assert(p != -1);
  Erase(p, q);
  // cout << p << ", " << q << endl;
  rep(i, 2) {
    while (p != root) {
      int np = prv[p];
      Erase(p, np);
      // cout << p << ", " << np << endl;
      p = np;
    }
    p = q;
  }
  return true;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(0);

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

  rep(v, N) {
    if (es[v].size() % 2) {
      puts("No");
      return 0;
    }
  }

  rep(i, 3) {
    // cout << i << endl;
    if (!check()) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
}


Fixed submission:
#include <bits/stdc++.h>
#define X first
#define Y second
#define pb emplace_back
#define FOR(i, a, b) for (int(i) = (a); i < (b); ++(i))
#define EFOR(i, a, b) for (int(i) = (a); i <= (b); ++(i))
#define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X))
#define REP rep
#define rrep(X, Y) for (int(X) = (Y)-1; (X) >= 0; --(X))
#define all(X) (X).begin(), (X).end()
#define eb emplace_back

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef ll LL;
typedef pii PII;
typedef pll PLL;
const ll MOD = 1e9 + 7;

#define rall(X) (X).rbegin(), (X).rend()
#define UNIQUE(X) (X).erase(unique(all(X)), (X).end())
#define reps(X, S, Y) for (int(X) = S; (X) < (Y); ++(X))
#define rreps(X, S, Y) for (int(X) = (Y)-1; (X) >= (S); --(X))
template <class T> inline bool MX(T &l, const T &r) {
  return l < r ? l = r, 1 : 0;
}
template <class T> inline bool MN(T &l, const T &r) {
  return l > r ? l = r, 1 : 0;
}

int N;
int M;
set<int> es[114514];
bool used[114514];
int prv[114514];
int depth[114514];

void Erase(int u, int v) {
  rep(i, 2) {
    assert(es[u].count(v));
    es[u].erase(v);
    swap(u, v);
  }
}

bool check() {
  fill(used, used + N, false);
  fill(prv, prv + N, -1);

  queue<int> que;
  int root = -1;
  rep(v, N) {
    if (!es[v].empty()) {
      root = v;
      used[v] = 1;
      que.push(v);
      break;
    }
  }
  if (root == -1)
    return false;

  using Ans = pair<int, pii>;
  Ans ans(MOD, pii(-1, -1));
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int u : es[v]) {
      if (!used[u]) {
        prv[u] = v;
        used[u] = true;
        depth[u] = depth[v] + 1;
        que.push(u);
      } else if (prv[v] != u) {
        MN(ans, Ans(1 + depth[v] + depth[u], pii(v, u)));
      }
    }
  }
  if (ans.X == MOD)
    return false;

  int p, q;
  tie(p, q) = ans.Y;
  assert(p != -1);
  Erase(p, q);
  if (depth[p] < depth[q])
    swap(p, q);
  while (depth[p] > depth[q]) {
    int np = prv[p];
    Erase(p, np);
    p = np;
  }

  while (p != q) {
    int np = prv[p];
    Erase(p, np);
    p = np;
    int nq = prv[q];
    Erase(q, nq);
    q = nq;
  }
  return true;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(0);

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

  rep(v, N) {
    if (es[v].size() % 2) {
      puts("No");
      return 0;
    }
  }

  rep(i, 3) {
    // cout << i << endl;
    if (!check()) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
}