#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int Maxn = 100005;

int n;
ll m;
int q;
int par[Maxn], siz[Maxn];
int a[Maxn], b[Maxn], c[Maxn];

int getPar(int x) { return par[x] == x? x: par[x] = getPar(par[x]); }

bool unionSet(int a, int b)
{
    a = getPar(a), b = getPar(b);
    if (a == b) return false;
    if (siz[a] < siz[b]) swap(a, b);
    siz[a] += siz[b]; par[b] = a;
    return true;
}

int main()
{
    scanf("%d %lld %d", &n, &m, &q);
    for (int i = 0; i < n; i++)
        par[i] = i, siz[i] = 1;
    bool was1 = false;
    for (int i = 0; i < q; i++) {
        scanf("%d %d %d", &a[i], &b[i], &c[i]);
        if (c[i] == 0) {
            if (unionSet(a[i], b[i])) m--;
        } else was1 = true;
    }
    for (int i = 0; i < q; i++) if (c[i] == 1)
        if (getPar(a[i]) == getPar(b[i])) { printf("No\n"); return 0; }
    int cnt = 0;
    for (int i = 0; i < n; i++)
        if (getPar(i) == i) cnt++;
    ll lef = ll(was1) + ll(cnt - 1);
    ll rig = ll(cnt) * (cnt - 1) / 2;
    if (lef <= m && m <= rig)
        printf("Yes\n");
    else printf("No\n");
    return 0;
}
