#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>

using namespace std;

int fa[100050];

struct fuck
{
	int u,v,w;
	bool operator < (const fuck &a) const
	{
		return w < a.w;
	}
}Q[100050];

int n;
long long m;

int find_fa(int pos)
{
	return fa[pos] == pos ? pos : fa[pos] = find_fa(fa[pos]);
}

int main()
{
	int q;
	scanf("%d%lld%d",&n,&m,&q);
	for(int i = 1;i <= q; ++ i)
		scanf("%d%d%d",&Q[i].u,&Q[i].v,&Q[i].w),Q[i].u ++,Q[i].v ++;
	sort(Q + 1,Q + 1 + q);
	for(int i = 1;i <= n; ++ i) fa[i] = i;
	int fuck = 1;
	for(int i = 1;i <= q; ++ i)
	{
		if(!Q[i].w)
		{
			int u = find_fa(Q[i].u),v = find_fa(Q[i].v);
			fa[u] = v;
		}
		else
		{
			fuck = 0;
			if(find_fa(Q[i].u) == find_fa(Q[i].v))
			{
				printf("No\n");
				return 0;
			}
		}
	}
	int cnt = 0;
	for(int i = 1;i <= n; ++ i) if(fa[i] == i) cnt ++;
	long long cmax = n - cnt + cnt * 1ll * (cnt - 1) / 2;
	if(cmax >= m && n - fuck <= m) printf("Yes\n");
	else printf("No\n");
	return 0;
}