#include<bits/stdc++.h>
using namespace std;
using ll=long long;
struct DSU{
	int n;
	vector<int> par,siz;
	DSU(int n):n(n){
		par.resize(n);
		siz.resize(n,1);
		for(int i=0;i<n;i++){
			par[i]=i;
		}
	}	
	int find(int x){
		if(par[x]==x){
			return x;
		}
		return par[x]=find(par[x]);
	}
	void unite(int u,int v){
		u=find(u),v=find(v);
		if(u==v){
			return;
		}
		if(siz[u]<siz[v]){
			swap(u,v);
		}
		siz[u]+=siz[v];
		par[v]=u;
	}
};
int main(){
	ll n,m,q;cin>>n>>m>>q;
	vector<int> a(q),b(q),c(q);
	DSU uf(n);
	for(int i=0;i<q;i++){
		cin>>a[i]>>b[i]>>c[i];
		if(c[i]==0){
			uf.unite(a[i],b[i]);
		}
	}
	ll s=0;
	for(int i=0;i<n;i++){
		if(uf.find(i)!=i){
			continue;
		}
		s++;
		m-=uf.siz[i]-1;
	}
	set<int> st;
	for(int i=0;i<q;i++){
		if(c[i]==1){
			if(uf.find(a[i])==uf.find(b[i])){
				cout<<"No"<<endl;
				return 0;
			}
			st.insert(uf.find(a[i]));
			st.insert(uf.find(b[i]));
		}
	}
	ll lb=st.size(),ub=s*(s-1LL)/2LL;
	//cout<<lb<<" "<<m<<" "<<ub<<endl;
	if(lb<=m&&m<=ub){
		cout<<"Yes"<<endl;
	}else{
		cout<<"No"<<endl;
	}
}
