Problem:
Score : 700 points

Problem Statement

Snuke's mother gave Snuke an undirected graph consisting of N vertices numbered 0 to N-1 and M edges.
This graph was connected and contained no parallel edges or self-loops.

One day, Snuke broke this graph.
Fortunately, he remembered Q clues about the graph.
The i-th clue (0 \leq i \leq Q-1) is represented as integers A_i,B_i,C_i and means the following:

If C_i=0: there was exactly one simple path (a path that never visits the same vertex twice) from Vertex A_i to B_i.

If C_i=1: there were two or more simple paths from Vertex A_i to B_i.

Snuke is not sure if his memory is correct, and worried whether there is a graph that matches these Q clues.
Determine if there exists a graph that matches Snuke's memory.

Constraints

2 \leq N \leq 10^5

N-1 \leq M \leq N \times (N-1)/2

1 \leq Q \leq 10^5

0 \leq A_i,B_i \leq N-1

A_i \neq B_i

0 \leq C_i \leq 1

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M Q
A_0 B_0 C_0
A_1 B_1 C_1
\vdots
A_{Q-1} B_{Q-1} C_{Q-1}

Output

If there exists a graph that matches Snuke's memory, print Yes; otherwise, print No.

Sample Input 1

5 5 3
0 1 0
1 2 1
2 3 0

Sample Output 1

Yes

For example, consider a graph with edges (0,1),(1,2),(1,4),(2,3),(2,4). This graph matches the clues.

Sample Input 2

4 4 3
0 1 0
1 2 1
2 3 0

Sample Output 2

No

Sample Input 3

10 9 9
7 6 0
4 5 1
9 7 0
2 9 0
2 3 0
4 1 0
8 0 0
9 1 0
3 0 0

Sample Output 3

No

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using ld = long double;

template <typename T>
int sz(T &x) {
  return (int)(x.size());
}

int power(int x, int y, int mod) {
  if (y == 0) {
    return 1;
  }
  int z = power(x, y >> 1, mod);
  z = z * 1ll * z % mod;
  if (y & 1) {
    z = z * 1ll * x % mod;
  }
  return z;
}

const int mod = 998244353;
const int inf = 1'000'000'007;

vector<int> p;

int find9(int x) {
  return p[x] == x ? x : p[x] = find9(p[x]);
}

void merge9(int x, int y) {
  x = find9(x);
  y = find9(y);
  if (x == y) {
    return;
  }
  p[x] = y;
}

void solve() {
  int n, k;
  ll m;
  cin >> n >> m >> k;

  p.clear(), p.resize(n), iota(p.begin(), p.end(), 0);
  vector<int> a(k), b(k), c(k);
  for (int i = 0; i < k; ++i) {
    cin >> a[i] >> b[i] >> c[i];
    if (c[i] == 0) {
      merge9(a[i], b[i]);
    }
  }



  set<int> s;
  for (int i = 0; i < n; ++i) {
    s.insert(find9(i));
  }


  for (int i = 0; i < k; ++i) {
    if (c[i] == 1) {
      if (m == n - 1) {
        cout << "No\n";
        return;
      }
      if (find9(a[i]) == find9(b[i])) {
        cout << "No\n";
        return;
      }
    }
  }



  ll ans = n - 1 + sz(s) * max(0ll, sz(s) - 3ll) / 2;

  if (ans < m) {
    cout << "No\n";
  } else {
    cout << "Yes\n";
  }
}

int main() {
  #ifndef LOCAL58
    // don't forget to remove if interactive
    ios_base :: sync_with_stdio(false);
    cin.tie(nullptr);
  #endif

  int t;
  t = 1;
  while (t--) {
    solve();
  }

  return 0;
}