#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int N=100005;

int n,m,q,ecnt,cnt1;
int fa[N];
vector<int>A,B;
int find(int x) {
	if(x==fa[x]) return x;
	return fa[x]=find(fa[x]);
}
int main() {
	scanf("%d %d %d",&n,&m,&q);
	for(int i=1;i<=n;i++) fa[i]=i;
	for(int i=1,u,v,c;i<=q;i++) {
		scanf("%d %d %d",&u,&v,&c);
		if(c==1) {
			A.push_back(u);
			B.push_back(v);
		}
		else {
			int fu=find(u);
			int fv=find(v);
			if(fu!=fv)
				fa[fu]=fv,ecnt++;
		}
	}
	for(int i=0;i<(int)A.size();i++) {
		int fu=find(A[i]);
		int fv=find(B[i]);
		if(fu==fv) {
			printf("No\n");
			return 0;
		}
	}
	long long l=n-ecnt;
	if(A.size()==0) {
		if(m<=ecnt+l*(l-1)/2)
			printf("Yes\n");
		else printf("No\n");
	}
	else {
		if(n<=m&&m<=ecnt+l*(l-1)/2) printf("Yes\n");
		else printf("No\n");
	}
	return 0;
}
