#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=1000005;
int a[maxn],b[maxn],c[maxn],n,m,q,f[maxn],mi,ma,tot[maxn];
int find(int x)
{
 return x==f[x]?x:f[x]=find(f[x]);
}
int max(int x,int y)
{
	return x>y?x:y;
}
signed main()
{
	scanf("%lld%lld%lld",&n,&m,&q);
	for(int i=1;i<=n;++i)f[i]=i;int flag=0;
	for(int i=1;i<=q;++i)
	{
		 scanf("%lld%lld%lld",&a[i],&b[i],&c[i]);a[i]++;b[i]++;
		 if(!c[i])
		 {
		 	int x=find(a[i]);int y=find(b[i]);
		 	if(x!=y)
			{
		 		f[y]=x;
			}
		 }
	}
	for(int i=1;i<=q;++i)
	{
		if(c[i])
		{
			flag=1;
			if(find(a[i])==find(b[i]))
			{
				puts("No");return 0;
			}
		}
	}
	int c=0;
	for(int i=1;i<=n;++i)
	{
		if(find(i)==i)
		{
	     c++;
		}
	}
	if(!flag)mi=c-1;
	else mi=max(3,c);
	ma=c*(c-1)/2;
	m-=(n-c);
	if(m>=mi&&m<=ma)puts("Yes");
	else puts("No");
}
