Problem p02906 (C++)

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

#define int long long

#define pii pair<int, int>

#define x1 x1228
#define y1 y1228

#define left left228
#define right right228

#define pb push_back
#define eb emplace_back

#define mp make_pair

#define ff first
#define ss second

#define matr vector<vector<int>>

#define all(x) x.begin(), x.end()

using namespace std;
typedef long long ll;
typedef long double ld;

const int maxn = 3e5 + 7, mod = 998244353, MAXN = 1e6 + 7;
const double eps = 1e-9;
const ll inf = 1e18;
mt19937 rnd(time(0));
int n, m, q;
struct edge {
  int a, b, c;
};

vector<edge> e;
int pr[maxn];
int siz[maxn];

int get(int a) {
  if (pr[a] == a)
    return a;
  return pr[a] = get(pr[a]);
}

int ctr;
bool unite(int a, int b) {
  a = get(a);
  b = get(b);
  if (a == b)
    return 0;
  if (siz[a] > siz[b])
    swap(a, b);
  pr[a] = b;
  siz[b] += siz[a];
  return 1;
}

void solve() {
  cin >> n >> m >> q;
  ctr = n;
  for (int i = 0; i < n; ++i) {
    pr[i] = i;
    siz[i] = 1;
  }
  set<pair<pii, int>> kek;
  for (int i = 0; i < q; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    if (a > b)
      swap(a, b);
    kek.insert(mp(mp(a, b), c));
  }
  for (auto v : kek) {
    e.pb({v.ff.ff, v.ff.ss, v.ss});
  }
  for (auto v : e) {
    if (v.c == 0) {
      int t = unite(v.a, v.b);
      ctr -= t;
    }
  }
  set<pii> ms;
  int ptr = 0;
  for (auto v : e) {
    if (v.c == 1) {
      ptr++;
      int a = get(v.a);
      int b = get(v.b);
      if (a == b) {
        cout << "No";
        return;
      }
      if (a > b)
        swap(a, b);
      ms.insert(mp(a, b));
    }
  }
  if (ms.size() != ptr) {
    cout << "No";
    return;
  }
  exit(1);
  if (ctr == 2) {
    if (ptr)
      cout << "No";
    else
      cout << "Yes";
    return;
  }
  if (m == n - 1) {
    if (ptr)
      cout << "No";
    else
      cout << "Yes";
    return;
  }
  if (m <= ctr * (ctr - 1) / 2 + n - ctr) {
    cout << "Yes";
    return;
  }
  cout << "No";
}

signed main() {
#ifdef LOCAL
  freopen("TASK.in", "r", stdin);
  freopen("TASK.out", "w", stdout);
#else

#endif // LOCAL
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(20);
  cout << fixed;
  int t = 1;
  for (int i = 0; i < t; ++i) {
    solve();
  }
  return 0;
}

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

#define int long long

#define pii pair<int, int>

#define x1 x1228
#define y1 y1228

#define left left228
#define right right228

#define pb push_back
#define eb emplace_back

#define mp make_pair

#define ff first
#define ss second

#define matr vector<vector<int>>

#define all(x) x.begin(), x.end()

using namespace std;
typedef long long ll;
typedef long double ld;

const int maxn = 3e5 + 7, mod = 998244353, MAXN = 1e6 + 7;
const double eps = 1e-9;
const ll inf = 1e18;
mt19937 rnd(time(0));
int n, m, q;
struct edge {
  int a, b, c;
};

vector<edge> e;
int pr[maxn];
int siz[maxn];

int get(int a) {
  if (pr[a] == a)
    return a;
  return pr[a] = get(pr[a]);
}

int ctr;
bool unite(int a, int b) {
  a = get(a);
  b = get(b);
  if (a == b)
    return 0;
  if (siz[a] > siz[b])
    swap(a, b);
  pr[a] = b;
  siz[b] += siz[a];
  return 1;
}

void solve() {
  cin >> n >> m >> q;
  ctr = n;
  for (int i = 0; i < n; ++i) {
    pr[i] = i;
    siz[i] = 1;
  }
  set<pair<pii, int>> kek;
  for (int i = 0; i < q; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    if (a > b)
      swap(a, b);
    kek.insert(mp(mp(a, b), c));
  }
  for (auto v : kek) {
    e.pb({v.ff.ff, v.ff.ss, v.ss});
  }
  for (auto v : e) {
    if (v.c == 0) {
      int t = unite(v.a, v.b);
      ctr -= t;
    }
  }
  set<pii> ms;
  int ptr = 0;
  for (auto v : e) {
    if (v.c == 1) {
      ptr++;
      int a = get(v.a);
      int b = get(v.b);
      if (a == b) {
        cout << "No";
        return;
      }
      if (a > b)
        swap(a, b);
      ms.insert(mp(a, b));
    }
  }
  if (ctr == 2) {
    if (ptr)
      cout << "No";
    else
      cout << "Yes";
    return;
  }
  if (m == n - 1) {
    if (ptr)
      cout << "No";
    else
      cout << "Yes";
    return;
  }
  if (m <= ctr * (ctr - 1) / 2 + n - ctr) {
    cout << "Yes";
    return;
  }
  cout << "No";
}

signed main() {
#ifdef LOCAL
  freopen("TASK.in", "r", stdin);
  freopen("TASK.out", "w", stdout);
#else

#endif // LOCAL
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(20);
  cout << fixed;
  int t = 1;
  for (int i = 0; i < t; ++i) {
    solve();
  }
  return 0;
}