#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5+7;
int u[N],v[N],c[N],group[N],num[N];
int f[N];
bool vis[N];
int sf(int x){
    return f[x]==x ? x : f[x] = sf(f[x]);
}
void un(int a,int b){
    f[sf(a)] = sf(b);
}
int main()
{
    ll m;
    ll Minedge=0,Maxedge=0;
    int n,q;
    scanf("%d%lld%d",&n,&m,&q);
    for(int i=0;i<=n;i++)f[i]=i;
    for(int i=0;i<q;i++){
        scanf("%d%d%d",&u[i],&v[i],&c[i]);
        if(c[i] == 0){
            un(u[i], v[i]);
        }
    }
    int cnt=0;
    for(int i=0;i<n;i++){
        if(!vis[sf(i)])cnt++;
        vis[sf(i)] = 1;
        group[i] = cnt;
        num[cnt]++;
    }
    bool flag = 0;
    for(int i=0;i<q;i++){
        if(c[i] == 1){
        	flag = 1;
        	if(sf(u[i]) == sf(v[i])){
        		puts("No");
        		return 0;
			}
        }
    }
    if(flag && cnt <= 2){
    	puts("No");return 0;
	}
	else{
		for(int i=1;i<=cnt;i++){
			Minedge += num[i] - 1;
		}
		Maxedge = Minedge + 1ll * cnt * (cnt-1) / 2;
		if(flag) Minedge += cnt;
		else Minedge += cnt - 1;
	}
	//Maxedge = Minedge + 1ll * cnt * (cnt-1) / 2 - cnt + (!flag ? 1 : 0);
    if(Minedge <= m && m <= Maxedge){
        puts("Yes");
    }
    else{
        puts("No");
    }
}