#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=100005;
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 hh=0;
	for(int i=1;i<=n;++i)
	{
		if(find(i)==i)
		{
	     hh++;
		}
	}
	if(!flag)mi=hh-1;
	else mi=max(3,hh);ma=hh*(hh-1)/2;
	m-=(n-hh);
	if(m>=mi&&m<=ma)puts("Yes");
	else puts("No");
}
