Problem p03091 (C++)

Buggy submission (status: Runtime Error):
// IO library
#include <cstdio>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm library
#include <algorithm>
#include <cmath>
#include <limits>
#include <numeric>
#include <random>

// contancer library
#include <bitset>
#include <deque>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using ll = long long;
using ld = long double;

/* ----- class ----- */

template <class Cost = int, class Cap = int> struct Edge {
  int from, to, rev;
  Cost cost;
  Cap cap;
  Edge(int from = -1, int to = -1, Cost cost = 1, Cap cap = 0, int rev = -1)
      : from(from), to(to), cost(cost), cap(cap), rev(rev){};

  bool operator<(const Edge<Cost> &e) const { return this->cost < e.cost; }
  bool operator>(const Edge<Cost> &e) const { return this->cost > e.cost; }
};

template <class Cost = int, class Cap = int> class Graph {
public:
  int size;
  std::vector<std::vector<Edge<Cost, Cap>>> path;

  explicit Graph(int N = 0) : size(N), path(size) {}
  void span(int u, int v, Cost cost = 1, Cap cap = 0) {
    path[u].push_back(Edge<Cost, Cap>(u, v, cost, cap));
  }
  std::vector<Edge<Cost, Cap>> operator[](int v) const { return path[v]; }
};

/* ----- Output Functions for Debugging ----- */

template <class T> std::ostream &operator<<(std::ostream &os, std::vector<T> v);
template <class T> std::ostream &operator<<(std::ostream &os, std::set<T> v);
template <class L, class R>
std::ostream &operator<<(std::ostream &os, std::pair<L, R> p);
template <class K, class T>
std::ostream &operator<<(std::ostream &os, std::map<K, T> v);
template <class T> std::ostream &operator<<(std::ostream &os, std::queue<T> q);
template <class T>
std::ostream &operator<<(std::ostream &os, std::priority_queue<T> q);
template <class T>
std::ostream &
operator<<(std::ostream &os,
           std::priority_queue<T, std::vector<T>, std::greater<T>> q);
template <class T, class U>
std::ostream &operator<<(std::ostream &os, Edge<T, U> e);

template <class T>
std::ostream &operator<<(std::ostream &os, std::vector<T> v) {
  os << "[";
  for (auto vv : v)
    os << vv << ",";
  return os << "]";
}
template <class T> std::ostream &operator<<(std::ostream &os, std::set<T> v) {
  os << "{";
  for (auto vv : v)
    os << vv << ",";
  return os << "}";
}
template <class L, class R>
std::ostream &operator<<(std::ostream &os, std::pair<L, R> p) {
  return os << "(" << p.first << "," << p.second << ")";
}
template <class K, class T>
std::ostream &operator<<(std::ostream &os, std::map<K, T> v) {
  os << "{";
  for (auto vv : v)
    os << vv << ",";
  return os << "}";
}
template <class T> std::ostream &operator<<(std::ostream &os, std::queue<T> q) {
  os << "[";
  while (!q.empty()) {
    os << q.front() << ",";
    q.pop();
  }
  return os << "]";
}
template <class T>
std::ostream &operator<<(std::ostream &os, std::priority_queue<T> q) {
  os << "{";
  while (!q.empty()) {
    os << q.top() << ",";
    q.pop();
  }
  return os << "}";
}
template <class T>
std::ostream &
operator<<(std::ostream &os,
           std::priority_queue<T, std::vector<T>, std::greater<T>> q) {
  os << "{";
  while (!q.empty()) {
    os << q.top() << ",";
    q.pop();
  }
  return os << "}";
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, Edge<T, U> e) {
  return os << "(" << e.from << "->" << e.to << ":" << e.cost << "," << e.cap
            << ")";
}

/* ----- Short Functions ----- */

template <class T> T Vec(T v) { return v; }

template <class T, class... Ts> auto Vec(size_t l, Ts... ts) {
  return std::vector<decltype(Vec<T>(ts...))>(l, Vec<T>(ts...));
}

template <class T> inline T sq(T a) { return a * a; }

template <class T> inline T iceil(T n, T d) { return (n + d - 1) / d; }

template <class T> T gcd(T a, T b) {
  while (b > 0) {
    a %= b;
    swap(a, b);
  }
  return a;
}

template <class T, class U> T ipow(T b, U n) {
  T ret = 1;
  while (n > 0) {
    if (n & 1)
      ret *= b;
    n >>= 1;
    b *= b;
  }
  return ret;
}

// 0-indexed
template <class T, class U> inline T kthbit(T a, U k) { return (a >> k) & 1; }

template <class T, class U> inline T mask(T a, U k) {
  return a & ((1 << k) - 1);
}

/* ----- Constants ----- */

// const int INF = std::numeric_limits<int>::max();
// const ll INF = std::numeric_limits<ll>::max();
// const ld PI = acos(-1);
// const ld EPS = 1e-10;
// std::mt19937 mt(ll(time(0)));

void fail() {
  std::cout << "No" << std::endl;
  exit(0);
}

void ok() {
  std::cout << "Yes" << std::endl;
  exit(0);
}

template <class Cost = int, class Cap = int> class Lowlink {
private:
  Graph<Cost, Cap> graph;
  int cur;
  std::vector<int> order, low;

  void dfs(int v, int r) {
    order[v] = low[v] = cur++;
    int deg = 0;
    bool is_artic = false;

    for (auto e : graph[v]) {
      if (order[e.to] < 0) {
        ++deg;
        dfs(e.to, e.from);
        low[e.from] = std::min(low[e.from], low[e.to]);

        if (order[e.from] <= low[e.to])
          is_artic = true;
        if (order[e.from] < low[e.to])
          bridges.push_back(e);
      } else if (e.to != r) {
        // eは後退辺 1回だけ遡る
        low[e.from] = std::min(low[e.from], order[e.to]);
      }
    }

    if (r < 0)
      is_artic = (deg > 1);
    if (is_artic)
      artics.push_back(v);
  }

public:
  std::vector<int> artics;
  std::vector<Edge<Cost, Cap>> bridges;

  explicit Lowlink(const Graph<Cost, Cap> &graph)
      : graph(graph), order(graph.size, -1), low(graph.size, graph.size) {
    cur = 0;
    for (int v = 0; v < graph.size; ++v) {
      if (order[v] < 0)
        dfs(v, -1);
    }
  }
};

int main() {
  int N, M;
  std::cin >> N >> M;

  Graph<> graph(N);
  for (int i = 0; i < M; ++i) {
    int u, v;
    std::cin >> u >> v;
    --u, --v;
    graph.span(u, v);
    graph.span(v, u);
  }

  for (int v = 0; v < N; ++v) {
    if (graph[v].size() % 2 != 0)
      fail();
  }

  for (int v = 0; v < N; ++v) {
    if (graph[v].size() >= 6)
      ok();
  }

  std::terminate();

  Lowlink<> lowlink(graph);
  std::cout << (lowlink.artics.empty() ? "No" : "Yes") << std::endl;
  return 0;
}


Fixed submission:
// IO library
#include <cstdio>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm library
#include <algorithm>
#include <cmath>
#include <limits>
#include <numeric>
#include <random>

// contancer library
#include <bitset>
#include <deque>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using ll = long long;
using ld = long double;

/* ----- class ----- */

template <class Cost = int, class Cap = int> struct Edge {
  int from, to, rev;
  Cost cost;
  Cap cap;
  Edge(int from = -1, int to = -1, Cost cost = 1, Cap cap = 0, int rev = -1)
      : from(from), to(to), cost(cost), cap(cap), rev(rev){};

  bool operator<(const Edge<Cost> &e) const { return this->cost < e.cost; }
  bool operator>(const Edge<Cost> &e) const { return this->cost > e.cost; }
};

template <class Cost = int, class Cap = int> class Graph {
public:
  int size;
  std::vector<std::vector<Edge<Cost, Cap>>> path;

  explicit Graph(int N = 0) : size(N), path(size) {}
  void span(int u, int v, Cost cost = 1, Cap cap = 0) {
    path[u].push_back(Edge<Cost, Cap>(u, v, cost, cap));
  }
  std::vector<Edge<Cost, Cap>> operator[](int v) const { return path[v]; }
};

/* ----- Output Functions for Debugging ----- */

template <class T> std::ostream &operator<<(std::ostream &os, std::vector<T> v);
template <class T> std::ostream &operator<<(std::ostream &os, std::set<T> v);
template <class L, class R>
std::ostream &operator<<(std::ostream &os, std::pair<L, R> p);
template <class K, class T>
std::ostream &operator<<(std::ostream &os, std::map<K, T> v);
template <class T> std::ostream &operator<<(std::ostream &os, std::queue<T> q);
template <class T>
std::ostream &operator<<(std::ostream &os, std::priority_queue<T> q);
template <class T>
std::ostream &
operator<<(std::ostream &os,
           std::priority_queue<T, std::vector<T>, std::greater<T>> q);
template <class T, class U>
std::ostream &operator<<(std::ostream &os, Edge<T, U> e);

template <class T>
std::ostream &operator<<(std::ostream &os, std::vector<T> v) {
  os << "[";
  for (auto vv : v)
    os << vv << ",";
  return os << "]";
}
template <class T> std::ostream &operator<<(std::ostream &os, std::set<T> v) {
  os << "{";
  for (auto vv : v)
    os << vv << ",";
  return os << "}";
}
template <class L, class R>
std::ostream &operator<<(std::ostream &os, std::pair<L, R> p) {
  return os << "(" << p.first << "," << p.second << ")";
}
template <class K, class T>
std::ostream &operator<<(std::ostream &os, std::map<K, T> v) {
  os << "{";
  for (auto vv : v)
    os << vv << ",";
  return os << "}";
}
template <class T> std::ostream &operator<<(std::ostream &os, std::queue<T> q) {
  os << "[";
  while (!q.empty()) {
    os << q.front() << ",";
    q.pop();
  }
  return os << "]";
}
template <class T>
std::ostream &operator<<(std::ostream &os, std::priority_queue<T> q) {
  os << "{";
  while (!q.empty()) {
    os << q.top() << ",";
    q.pop();
  }
  return os << "}";
}
template <class T>
std::ostream &
operator<<(std::ostream &os,
           std::priority_queue<T, std::vector<T>, std::greater<T>> q) {
  os << "{";
  while (!q.empty()) {
    os << q.top() << ",";
    q.pop();
  }
  return os << "}";
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, Edge<T, U> e) {
  return os << "(" << e.from << "->" << e.to << ":" << e.cost << "," << e.cap
            << ")";
}

/* ----- Short Functions ----- */

template <class T> T Vec(T v) { return v; }

template <class T, class... Ts> auto Vec(size_t l, Ts... ts) {
  return std::vector<decltype(Vec<T>(ts...))>(l, Vec<T>(ts...));
}

template <class T> inline T sq(T a) { return a * a; }

template <class T> inline T iceil(T n, T d) { return (n + d - 1) / d; }

template <class T> T gcd(T a, T b) {
  while (b > 0) {
    a %= b;
    swap(a, b);
  }
  return a;
}

template <class T, class U> T ipow(T b, U n) {
  T ret = 1;
  while (n > 0) {
    if (n & 1)
      ret *= b;
    n >>= 1;
    b *= b;
  }
  return ret;
}

// 0-indexed
template <class T, class U> inline T kthbit(T a, U k) { return (a >> k) & 1; }

template <class T, class U> inline T mask(T a, U k) {
  return a & ((1 << k) - 1);
}

/* ----- Constants ----- */

// const int INF = std::numeric_limits<int>::max();
// const ll INF = std::numeric_limits<ll>::max();
// const ld PI = acos(-1);
// const ld EPS = 1e-10;
// std::mt19937 mt(ll(time(0)));

void fail() {
  std::cout << "No" << std::endl;
  exit(0);
}

void ok() {
  std::cout << "Yes" << std::endl;
  exit(0);
}

template <class Cost = int, class Cap = int> class Lowlink {
private:
  Graph<Cost, Cap> graph;
  int cur;
  std::vector<int> order, low;

  void dfs(int v, int r) {
    order[v] = low[v] = cur++;
    int deg = 0;
    bool is_artic = false;

    for (auto e : graph[v]) {
      if (order[e.to] < 0) {
        ++deg;
        dfs(e.to, e.from);
        low[e.from] = std::min(low[e.from], low[e.to]);

        if (order[e.from] <= low[e.to])
          is_artic = true;
        if (order[e.from] < low[e.to])
          bridges.push_back(e);
      } else if (e.to != r) {
        // eは後退辺 1回だけ遡る
        low[e.from] = std::min(low[e.from], order[e.to]);
      }
    }

    if (r < 0)
      is_artic = (deg > 1);
    if (is_artic)
      artics.push_back(v);
  }

public:
  std::vector<int> artics;
  std::vector<Edge<Cost, Cap>> bridges;

  explicit Lowlink(const Graph<Cost, Cap> &graph)
      : graph(graph), order(graph.size, -1), low(graph.size, graph.size) {
    cur = 0;
    for (int v = 0; v < graph.size; ++v) {
      if (order[v] < 0)
        dfs(v, -1);
    }
  }
};

int main() {
  int N, M;
  std::cin >> N >> M;

  Graph<> graph(N);
  for (int i = 0; i < M; ++i) {
    int u, v;
    std::cin >> u >> v;
    --u, --v;
    graph.span(u, v);
    graph.span(v, u);
  }

  for (int v = 0; v < N; ++v) {
    if (graph[v].size() % 2 != 0)
      fail();
  }

  for (int v = 0; v < N; ++v) {
    if (graph[v].size() >= 6)
      ok();
  }

  int cnt = 0;
  for (int v = 0; v < N; ++v) {
    if (graph[v].size() >= 4)
      ++cnt;
  }

  if (cnt <= 1)
    fail();
  if (cnt >= 3)
    ok();

  Lowlink<> lowlink(graph);
  std::cout << (lowlink.artics.empty() ? "No" : "Yes") << std::endl;
  return 0;
}