Problem p03091 (C++)

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

using i64 = int64_t;

#define rep(i, x, y)                                                           \
  for (i64 i = i64(x), i##_max_for_repmacro = i64(y);                          \
       i < i##_max_for_repmacro; ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1.01e9;
const i64 inf64 = 4.01e18;
const double eps = 1e-9;

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class graph_type> class bridge_and_articulation {
private:
  void dfs(int v, int p, int &k) { // vが根のときp=-1
    used[v] = true;
    ord[v] = low[v] = k++;

    int count = 0;
    for (const auto &edge : graph[v]) {
      const int u = edge.to;
      if (!used[u]) {
        ++count;
        dfs(u, v, k);
        low[v] = min(low[v], low[u]);

        if (ord[v] < low[u])
          bridges.push_back(make_pair(min(v, u), max(v, u)));
        if (p != -1 and ord[v] <= low[u])
          is_art[v] = true;
      } else if (u != p)
        low[v] = min(low[v], ord[u]);
    }

    if (p == -1 and count > 1)
      is_art[v] = true;
    if (is_art[v])
      articulations.push_back(v);
  }

public:
  const int size;
  graph_type graph;
  vector<bool> used, is_art;
  vector<int> ord, low, articulations;
  vector<pair<int, int>> bridges;

  bridge_and_articulation(const graph_type &graph)
      : graph(graph), size(graph.size()), used(size), is_art(size), ord(size),
        low(size) {
    int k = 0;
    for (int i = 0; i < size; ++i)
      if (!used[i])
        dfs(i, -1, k);
  }
  bool is_articulation(int v) const { return is_art[v]; }
  bool is_bridge(int u, int v) const { // 辺(u,v)がグラフに含まれることを仮定する
    if (ord[u] > ord[v])
      swap(u, v);
    return ord[u] < low[v];
  }
};

struct edge {
  i64 to;
};

void solve() {
  // const i64 mod = 1'000'000'007;
  i64 N, M;
  cin >> N >> M;

  vector<i64> a(M), b(M);
  vector<vector<i64>> graph(N);
  vector<i64> deg(N);
  rep(i, 0, M) {
    cin >> a[i] >> b[i];
    --a[i];
    --b[i];
    graph[a[i]].emplace_back(b[i]);
    graph[b[i]].emplace_back(a[i]);
    ++deg[a[i]];
    ++deg[b[i]];
  }

  const string yes = "Yes", no = "No";

  rep(i, 0, N) {
    if (deg[i] % 2 != 0) {
      cout << no << endl;
      return;
    }
  }

  rep(i, 0, N) {
    if (deg[i] >= 6) {
      cout << yes << endl;
      return;
    }
  }

  map<i64, i64> cnt;
  rep(i, 0, N) { ++cnt[deg[i]]; }

  if (cnt[4] <= 1) {
    cout << no << endl;
    return;
  }

  if (cnt[4] == 2) {
    vector<vector<edge>> g(N);
    rep(i, 0, N) {
      for (auto j : graph[i]) {
        g[i].emplace_back(edge{j});
      }
    }
    bridge_and_articulation<vector<vector<edge>>> ba(g);
    rep(i, 0, N) {
      if (ba.is_articulation(i)) {
        cout << yes << endl;
        return;
      }
    }
    cout << no << endl;
  }

  if (cnt[4] >= 3) {
    cout << yes << endl;
    return;
  }

  assert(false);
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(16);
  solve();
  return 0;
}


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

using i64 = int64_t;

#define rep(i, x, y)                                                           \
  for (i64 i = i64(x), i##_max_for_repmacro = i64(y);                          \
       i < i##_max_for_repmacro; ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1.01e9;
const i64 inf64 = 4.01e18;
const double eps = 1e-9;

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class graph_type> class bridge_and_articulation {
private:
  void dfs(int v, int p, int &k) { // vが根のときp=-1
    used[v] = true;
    ord[v] = low[v] = k++;

    int count = 0;
    for (const auto &edge : graph[v]) {
      const int u = edge.to;
      if (!used[u]) {
        ++count;
        dfs(u, v, k);
        low[v] = min(low[v], low[u]);

        if (ord[v] < low[u])
          bridges.push_back(make_pair(min(v, u), max(v, u)));
        if (p != -1 and ord[v] <= low[u])
          is_art[v] = true;
      } else if (u != p)
        low[v] = min(low[v], ord[u]);
    }

    if (p == -1 and count > 1)
      is_art[v] = true;
    if (is_art[v])
      articulations.push_back(v);
  }

public:
  const int size;
  graph_type graph;
  vector<bool> used, is_art;
  vector<int> ord, low, articulations;
  vector<pair<int, int>> bridges;

  bridge_and_articulation(const graph_type &graph)
      : graph(graph), size(graph.size()), used(size), is_art(size), ord(size),
        low(size) {
    int k = 0;
    for (int i = 0; i < size; ++i)
      if (!used[i])
        dfs(i, -1, k);
  }
  bool is_articulation(int v) const { return is_art[v]; }
  bool is_bridge(int u, int v) const { // 辺(u,v)がグラフに含まれることを仮定する
    if (ord[u] > ord[v])
      swap(u, v);
    return ord[u] < low[v];
  }
};

struct edge {
  i64 to;
};

void solve() {
  // const i64 mod = 1'000'000'007;
  i64 N, M;
  cin >> N >> M;

  vector<i64> a(M), b(M);
  vector<vector<i64>> graph(N);
  vector<i64> deg(N);
  rep(i, 0, M) {
    cin >> a[i] >> b[i];
    --a[i];
    --b[i];
    graph[a[i]].emplace_back(b[i]);
    graph[b[i]].emplace_back(a[i]);
    ++deg[a[i]];
    ++deg[b[i]];
  }

  const string yes = "Yes", no = "No";

  rep(i, 0, N) {
    if (deg[i] % 2 != 0) {
      cout << no << endl;
      return;
    }
  }

  rep(i, 0, N) {
    if (deg[i] >= 6) {
      cout << yes << endl;
      return;
    }
  }

  map<i64, i64> cnt;
  rep(i, 0, N) { ++cnt[deg[i]]; }

  if (cnt[4] <= 1) {
    cout << no << endl;
    return;
  }

  if (cnt[4] == 2) {
    vector<vector<edge>> g(N);
    rep(i, 0, N) {
      for (auto j : graph[i]) {
        g[i].emplace_back(edge{j});
      }
    }
    bridge_and_articulation<vector<vector<edge>>> ba(g);
    rep(i, 0, N) {
      if (ba.is_articulation(i)) {
        cout << yes << endl;
        return;
      }
    }
    cout << no << endl;
    return;
  }

  if (cnt[4] >= 3) {
    cout << yes << endl;
    return;
  }

  assert(false);
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(16);
  solve();
  return 0;
}