#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 100010;
int fa[N], A[N], B[N], C[N];
int find(int x) {return x == fa[x] ? x : fa[x] = find(fa[x]);}
inline void merge(int x, int y) {fa[find(x)] = find(y);}
int main() {
    int n, q, flg = 0, ct = 0; LL m, L, R = 0; cin >> n >> m >> q;
    for (int i = 1; i <= n; i++) fa[i] = i;
    for (int i = 1; i <= q; i++) {
        cin >> A[i] >> B[i] >> C[i], A[i]++, B[i]++;
        if (!C[i]) merge(A[i], B[i]);
        else flg = 1;
    }
    for (int i = 1; i <= q; i++) if (C[i] && find(A[i]) == find(B[i])) {
        puts("No"); return 0;
    }
    for (int i = 1; i <= n; i++) if (fa[i] == i) ct++;
    L = n - (!flg), R = n - ct + (LL)ct * (ct - 1) / 2;
    if (m < L || m > R) puts("No");
    else puts("Yes");
    return 0;
}//