#include<bits/stdc++.h>
#define sz(v) ((int)(v).size())
using namespace std;
using lint = long long;
using pi = pair<int, int>;
const int MAXN = 100005;

struct disj{
	int pa[MAXN];
	void init(int n){
		iota(pa, pa + n + 1, 0);
	}
	int find(int x){
		return pa[x] = (pa[x] == x ? x : find(pa[x]));
	}
	bool uni(int p, int q){
		p = find(p);
		q = find(q);
		if(p == q) return 0;
		pa[q] = p; return 1;
	}
}disj;

int n; lint m; int q;
vector<pi>v;
int main(){
	scanf("%d %lld %d",&n,&m, &q);
	disj.init(n);
	int compCnt = n;
	while(q--){
		int a, b, c;
		scanf("%d %d %d",&a,&b,&c);
		if(c == 0){
			compCnt -= disj.uni(a, b);
		}
		else{
			v.emplace_back(a, b);
		}
	}
	for(auto &i : v){
		if(disj.find(i.first) == disj.find(i.second)){
			puts("No");
			return 0;
		}
	}
	if(m == n - 1){
		if(sz(v)) puts("No");
		else puts("Yes");
		return 0;
	}
	if(compCnt == 1){
		puts("No");
		return 0;
	}
	if(compCnt == 2){
		puts("No");
		return 0;
	}
	lint st = n - 1;
	lint ed = n - compCnt + 1ll * (compCnt) * (compCnt - 1) / 2;
	if(st <= m && m <= ed){
		puts("Yes");
	}
	else puts("No");
}
