Problem p02906 (C++)

Buggy submission (status: Runtime Error):
#include <cassert>
#include <cmath>
#include <iostream>
#include <vector>

using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))

class Dsu {
public:
  std::vector<int> mult;
  std::vector<int> sz;
  Dsu(int n) {
    mult.resize(1 + n);
    sz.resize(1 + n);
    for (int i = 1; i <= n; i++) {
      mult[i] = i;
      sz[i] = 1;
    }
  }
  int jump(int gala) {
    if (gala != mult[gala])
      mult[gala] = jump(mult[gala]);
    return mult[gala];
  }
  void unite(int a, int b) {
    a = jump(a);
    b = jump(b);
    if (a == b)
      return;
    if (sz[b] < sz[a])
      std::swap(a, b);
    sz[b] += sz[a];
    mult[a] = b;
  }
  int _count(int n) {
    int result = 0;
    for (int i = 1; i <= n; i++)
      if (mult[i] == i)
        result++;
    return result;
  }
};

int main() {
  int n, m, q;
  std::cin >> n >> m >> q;
  std::vector<std::pair<int, int>> cond[2];

  for (int i = 1; i <= q; i++) {
    int x, y, k;
    std::cin >> x >> y >> k;
    ++x;
    ++y;
    cond[k].push_back({x, y});
  }
  Dsu dsu(n);
  for (int i = 0; i < cond[0].size(); i++) {
    int x = cond[0][i].first;
    int y = cond[0][i].second;
    dsu.unite(x, y);
  }
  int comp = dsu._count(n);
  m -= (n - 1);
  if (m <= 1LL * comp * (comp - 1) / 2 - (comp - 1)) {
    if (0 < cond[1].size()) {
      if (0 == m || comp <= 2) {
        std::cout << "No";
        return 0;
      } else {
        for (int i = 0; i < cond[1].size(); i++) {
          int x = cond[1][i].first, y = cond[1][i].second;
          if (dsu.jump(x) == dsu.jump(y)) {
            std::cout << "No";
            return 0;
          }
        }
        std::cout << "Yes";
      }
    } else
      std::cout << "Yes";
  } else
    std::cout << "No";
  return 0;
}


Fixed submission:
#include <cassert>
#include <cmath>
#include <iostream>
#include <vector>

using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))

class Dsu {
public:
  std::vector<int> mult;
  std::vector<int> sz;
  Dsu(int n) {
    mult.resize(1 + n);
    sz.resize(1 + n);
    for (int i = 1; i <= n; i++) {
      mult[i] = i;
      sz[i] = 1;
    }
  }
  int jump(int gala) {
    if (gala != mult[gala])
      mult[gala] = jump(mult[gala]);
    return mult[gala];
  }
  void unite(int a, int b) {
    a = jump(a);
    b = jump(b);
    if (a == b)
      return;
    if (sz[b] < sz[a])
      std::swap(a, b);
    sz[b] += sz[a];
    mult[a] = b;
  }
  int _count(int n) {
    int result = 0;
    for (int i = 1; i <= n; i++)
      if (mult[i] == i)
        result++;
    return result;
  }
};

int main() {
  ll n, m, q;
  std::cin >> n >> m >> q;
  std::vector<std::pair<int, int>> cond[2];

  for (int i = 1; i <= q; i++) {
    int x, y, k;
    std::cin >> x >> y >> k;
    ++x;
    ++y;
    cond[k].push_back({x, y});
  }
  Dsu dsu(n);
  for (int i = 0; i < cond[0].size(); i++) {
    int x = cond[0][i].first;
    int y = cond[0][i].second;
    dsu.unite(x, y);
  }
  int comp = dsu._count(n);
  m -= (n - 1);
  if (m <= 1LL * comp * (comp - 1) / 2 - (comp - 1)) {
    if (0 < cond[1].size()) {
      if (0 == m || comp <= 2) {
        std::cout << "No";
        return 0;
      } else {
        for (int i = 0; i < cond[1].size(); i++) {
          int x = cond[1][i].first, y = cond[1][i].second;
          if (dsu.jump(x) == dsu.jump(y)) {
            std::cout << "No";
            return 0;
          }
        }
        std::cout << "Yes";
      }
    } else
      std::cout << "Yes";
  } else
    std::cout << "No";
  return 0;
}