Problem p03091 (C++)

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

using namespace std;
using LL = long long;
const LL LINF = 1e18;
const LL MOD = 998244353;

class UnionFind {
public:
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool connect(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> zisuu(N);
  vector<pair<int, int>> E;
  int cnt1 = 0;
  int cnt2 = 0;
  for (int a = 0; a < M; a++) {
    int b, c;
    cin >> b >> c;
    b--, c--;
    E.push_back({b, c});
    zisuu.at(b)++;
    zisuu.at(c)++;
  }
  bool r = true;
  for (int a = 0; a < N; a++) {
    if (zisuu.at(a) % 2 == 1)
      r = false;
    if (zisuu.at(a) >= 6)
      cnt1++;
    if (zisuu.at(a) == 4)
      cnt2++;
  }
  if (r) {
    if (cnt1 || cnt2 >= 3) {
      cout << "Yes" << endl;
    } else if (cnt2 == 2) {
      vector<int> flag;
      for (int a = 0; a < N; a++) {
        if (zisuu.at(a) == 4) {
          flag.push_back(a);
        }
      }
      for (auto x : flag) {
        UnionFind Uni(N);
        for (int a = 0; a < E.size(); a++) {
          if (x != E.at(a).first && x != E.at(a).second) {
            Uni.connect(E.at(a).first, E.at(a).second);
          }
        }

        if (Uni.size(0) == N - 1) {
          cout << "No" << endl;
          return 0;
        }
      }
      abort();
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  } else {
    cout << "No" << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using LL = long long;
const LL LINF = 1e18;
const LL MOD = 998244353;

class UnionFind {
public:
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool connect(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> zisuu(N);
  vector<pair<int, int>> E;
  int cnt1 = 0;
  int cnt2 = 0;
  for (int a = 0; a < M; a++) {
    int b, c;
    cin >> b >> c;
    b--, c--;
    E.push_back({b, c});
    zisuu.at(b)++;
    zisuu.at(c)++;
  }
  bool r = true;
  for (int a = 0; a < N; a++) {
    if (zisuu.at(a) % 2 == 1)
      r = false;
    if (zisuu.at(a) >= 6)
      cnt1++;
    if (zisuu.at(a) == 4)
      cnt2++;
  }
  if (r) {
    if (cnt1 || cnt2 >= 3) {
      cout << "Yes" << endl;
    } else if (cnt2 == 2) {
      vector<int> flag;
      for (int a = 0; a < N; a++) {
        if (zisuu.at(a) == 4) {
          flag.push_back(a);
        }
      }
      for (auto x : flag) {
        UnionFind Uni(N);
        for (int a = 0; a < E.size(); a++) {
          if (x != E.at(a).first && x != E.at(a).second) {
            Uni.connect(E.at(a).first, E.at(a).second);
          }
        }

        if (Uni.size(0) == N - 1) {
          cout << "No" << endl;
          return 0;
        }
      }
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  } else {
    cout << "No" << endl;
  }
}