#include <cstdio>
#include <vector>
#include <utility>
using namespace std;
typedef pair<int,int> ii;

int n,q;
long long m;

int p[100005];
long long components;

int parent(int i){return (p[i]==i)?i:p[i]=parent(p[i]);}

vector<ii> diff;

int main(){
	for (int x=0;x<100005;x++) p[x]=x;
	
	scanf("%d%lld%d",&n,&m,&q);
	components=n;
	
	int a,b,c;
	while (q--){
		scanf("%d%d%d",&a,&b,&c);
		if (c==0){
			a=parent(a),b=parent(b);
			if (a!=b){
				components--;
				p[a]=b;
			}
		}
		else{
			diff.push_back(ii(a,b));
		}
	}
	
	if (!diff.empty() && components<=2){
		printf("No");
		return 0;
	}
	
	for (auto &it:diff){
		if (parent(it.first)==parent(it.second)){
			printf("No");
			return 0;
		}
	}
	
	long long lo=n-(diff.empty()),hi=(components*(components-1))/2+n-components;
	
	//printf("%d %d %d\n",lo,hi,components);
	
	printf((lo<=m && m<=hi)?"Yes":"No");
}