#include <bits/stdc++.h>
#define x first
#define y second

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;

int n, m, q, comp;
int par[100100];

void init() {
    for (int i=0;i<n;i++) par[i] = i;
}
int fin(int a) {
    return par[a] = (par[a]==a?a:fin(par[a]));
}
void uni(int a, int b){
    a = fin(a); b = fin(b);
    if (a==b) return;
    comp--;
    par[fin(a)] = fin(b);
}
void pung() {
    puts("No");
    exit(0);
}
void print(bool t) {
    puts(t?"Yes":"No");
    exit(0);
}

int main() {
    scanf("%d%d%d",&n,&m,&q);
    init(); comp = n;
    vector<pii> vec;
    for (int i=0;i<q;i++) {
        int a, b, c;
        scanf("%d%d%d",&a,&b,&c);
        if (c==0) uni(a,b);
        else {
            vec.push_back({a,b});
        }
    }
    for (pii &v : vec) {
        if (fin(v.first)==fin(v.second)) pung();
    }
    if (comp==1) {
        print(m==n-1);
    }
    if (comp==2) {
        print(vec.empty());
    }
    if (vec.empty()) {
        print(m<=n-comp+1LL*comp*(comp-1)/2);
    }
    print(n<=m&&m<=n-comp+1LL*comp*(comp-1)/2);

	return 0;
}
