#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=114514;
int fa[N],u[N],v[N],t[N];
int n,q;
ll num,cnt,m;
int find(int x){
	return fa[x]==x?x:fa[x]=find(fa[x]);
}
void merge(int x,int y){
	if(x!=y) num--,fa[x]=y,cnt++;
} 
int main(){
	scanf("%d%d%d",&n,&m,&q);num=n;
	for(int i=1;i<=n;i++)fa[i]=i;
	for(int i=1;i<=q;i++){
		scanf("%d%d%d",&u[i],&v[i],&t[i]);u[i]++,v[i]++;
		if(!t[i]) merge(find(u[i]),find(v[i])),q--,i--;
	}
	if(m<=n-1){
		if(m<n-1)puts("No");
		else if(!q) puts("Yes");
		else puts("No");
		return 0;
	}for(int i=1;i<=q;i++){
		if(find(u[i])==find(v[i])){
			puts("No");return 0;
		}
	}ll r=1ll*cnt+((num*(num-1))/2);
	if(m>=n&&m<=r)puts("Yes");
	else puts("No");
	return 0;
}