Problem p02906 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define clr(x) memset(x, 0, sizeof x)
#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define Fod(i, b, a) for (int i = (b); i >= (a); i--)
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
#define next Next
#define y1 _y1
#define outval(x) cerr << #x " = " << x << endl
#define outtag(x) cerr << "-----------------" #x "-----------------\n"
#define outarr(a, L, R)                                                        \
  cerr << #a "[" << L << ".." << R << "] = ";                                  \
  For(_x, L, R) cerr << a[_x] << " ";                                          \
  cerr << endl;
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
LL read() {
  LL x = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch))
    f = ch == '-', ch = getchar();
  while (isdigit(ch))
    x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  return f ? -x : x;
}
const int N = 100005;
int n, q;
LL m;
struct nd {
  int a, b, c;
} v[N];
int fa[N];
int getf(int x) { return fa[x] == x ? x : fa[x] = getf(fa[x]); }
int id[N], c[N];
int cnt[N];
int main() {
  n = read(), m = read(), q = read();
  For(i, 1, q) v[i].a = read() + 1, v[i].b = read() + 1, v[i].c = read();
  For(i, 1, n) fa[i] = i;
  For(i, 1, q) {
    if (v[i].c == 0)
      fa[getf(v[i].a)] = getf(v[i].b);
  }
  For(i, 1,
      q) if (v[i].c == 1 && getf(v[i].a) == getf(v[i].b)) return puts("No"),
      0;
  int c1 = 0;
  For(i, 1, n) {
    id[i] = getf(i);
    c[id[i]]++;
    if (id[i] != i)
      c1++;
  }
  if (m < c1)
    return puts("No"), 0;
  For(i, 1, n) fa[i] = i;
  For(i, 1, q) if (v[i].c == 1) fa[getf(id[v[i].a])] = getf(id[v[i].b]);
  int s = 0;
  For(i, 1, n) if (id[i] == i) cnt[getf(i)]++, s++;
  vector<int> v;
  For(i, 1, n) if (id[i] == i && getf(i) == i) v.pb(cnt[i]);
  sort(v.begin(), v.end());
  int f = 0;
  if (s == 1)
    return puts(m == n - 1 ? "Yes" : "No");
  if (s == 2)
    return puts("No"), 0;
  if (m < n || m > (LL)s * (s - 1) / 2 + c1)
    return puts("No"), 0;
  puts("Yes");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define clr(x) memset(x, 0, sizeof x)
#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define Fod(i, b, a) for (int i = (b); i >= (a); i--)
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
#define next Next
#define y1 _y1
#define outval(x) cerr << #x " = " << x << endl
#define outtag(x) cerr << "-----------------" #x "-----------------\n"
#define outarr(a, L, R)                                                        \
  cerr << #a "[" << L << ".." << R << "] = ";                                  \
  For(_x, L, R) cerr << a[_x] << " ";                                          \
  cerr << endl;
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
LL read() {
  LL x = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch))
    f = ch == '-', ch = getchar();
  while (isdigit(ch))
    x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  return f ? -x : x;
}
const int N = 100005;
int n, q;
LL m;
struct nd {
  int a, b, c;
} v[N];
int fa[N];
int getf(int x) { return fa[x] == x ? x : fa[x] = getf(fa[x]); }
int id[N], c[N];
int cnt[N];
int main() {
  n = read(), m = read(), q = read();
  For(i, 1, q) v[i].a = read() + 1, v[i].b = read() + 1, v[i].c = read();
  For(i, 1, n) fa[i] = i;
  For(i, 1, q) {
    if (v[i].c == 0)
      fa[getf(v[i].a)] = getf(v[i].b);
  }
  For(i, 1,
      q) if (v[i].c == 1 && getf(v[i].a) == getf(v[i].b)) return puts("No"),
      0;
  int c1 = 0;
  For(i, 1, n) {
    id[i] = getf(i);
    c[id[i]]++;
    if (id[i] != i)
      c1++;
  }
  if (m < c1)
    return puts("No"), 0;
  For(i, 1, n) fa[i] = i;
  For(i, 1, q) if (v[i].c == 1) fa[getf(id[v[i].a])] = getf(id[v[i].b]);
  int s = 0;
  For(i, 1, n) if (id[i] == i) cnt[getf(i)]++, s++;
  vector<int> v;
  For(i, 1, n) if (id[i] == i && getf(i) == i) v.pb(cnt[i]);
  sort(v.begin(), v.end());
  if (v.back() == 1)
    return puts(m == n - 1 ? "Yes" : "No"), 0;
  if (s <= 2)
    return puts("No"), 0;
  if (m < n || m > (LL)s * (s - 1) / 2 + c1)
    return puts("No"), 0;
  puts("Yes");
  return 0;
}