#include <cstdio>

#include <algorithm>
#include <functional>
#include <vector>

using namespace std;

typedef long long ll;

int main() {
  int n, q;
  ll m;
  scanf("%d%lld%d", &n, &m, &q);
  vector<int> a(q), b(q), c(q), f(n);
  for (int i = 0; i < n; ++i)
    f[i] = i;
  for (int i = 0; i < q; ++i)
    scanf("%d%d%d", &a[i], &b[i], &c[i]);
  function<int(int)> find = [&](int x) {
    return f[x] == x ? x : f[x] = find(f[x]);
  };
  int cnt = n;
  for (int i = 0; i < q; ++i) {
    if (c[i] == 0) {
      int x = find(a[i]), y = find(b[i]);
      if (x != y) {
        if (x > y) swap(x, y);
        f[y] = x;
        --cnt;
      }
    }
  }
  if (cnt == 2) {
    for (int i = 1; i < n; ++i)
      if (find(i) != 0) {
        f[i] = 0;
        break;
      }
    cnt = 1;
  }
  if (n - cnt + cnt * (cnt - 1LL) / 2 < m) {
    puts("No");
    return 0;
  }
  if (m == n - 1) {
    puts(count(c.begin(), c.end(), 1) ? "No" : "Yes");
    return 0;
  }
  for (int i = 0; i < q; ++i)
    if (c[i] && find(a[i]) == find(b[i])) {
      puts("No");
      return 0;
    }
  puts("Yes");
  return 0;
}
