#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
ll fx,fy,num=0,siz=0,r,n,x,y,lim,l,z,i,m,q,c[2][100005],tot=0,dsu[100005],vis[100005];
ll getf(ll x){return dsu[x]==x?x:dsu[x]=getf(dsu[x]);}
ll wan(ll x){return x*(x-1LL)/2LL;}
int main()
{
	scanf("%lld%lld%lld",&n,&m,&q);
	for(i=1;i<=n;i++)dsu[i]=i;
	for(i=1;i<=q;i++)
	{
		scanf("%lld%lld%lld",&x,&y,&z);
		x++;y++;
		if(z==0)
		{
			vis[x]=vis[y]=1;
			fx=getf(x);
			fy=getf(y);
			if(fx!=fy)dsu[fx]=fy;
		}
		else
		{
			tot++;
			c[0][tot]=x;
			c[1][tot]=y;
		}
	}
	for(i=1;i<=tot;i++)
	{
		fx=getf(c[0][i]);fy=getf(c[1][i]);
		if(fx==fy)
		{
			printf("No\n");
			return 0;
		}
	}
	for(i=1;i<=n;i++)
	{
		if(vis[i])siz++;
		if(vis[i]&&getf(i)==i)num++;
	}
	if(num>=3)lim=siz-num+wan(num+n-siz);
	if(num==2)
	{
		if(siz==n&&tot>0)
		{
			printf("No\n");
			return 0;
		}
		lim=siz-num+wan(num+n-siz);
	}
	if(num==1)
	{
		if(siz==n-1&&tot>0)
		{
			printf("No\n");
			return 0;
		}
		lim=siz-num+wan(num+n-siz);
	}
	if(num==0)lim=siz-num+wan(n+num-siz);
	if(tot==0)l=n-1;
	else l=n;
	if(m<=lim&&m>=l)printf("Yes\n");
	else printf("No\n");
	return 0;
}