#include <cstdio>
#include <vector>
#include <iostream>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAXN = 100000;
vector<pii>vec;
int fa[MAXN + 5];
int find(int x) {
	return fa[x] = (fa[x] == x ? x : find(fa[x]));
}
void unite(int x, int y) {
	int fx = find(x), fy = find(y);
	if( fx != fy )
		fa[fx] = fy;
}
int N, Q; ll M;
int main() {
	scanf("%d%lld%d", &N, &M, &Q);
	for(int i=1;i<=N;i++) fa[i] = i;
	for(int i=1;i<=Q;i++) {
		int A, B, C; scanf("%d%d%d", &A, &B, &C), A++, B++;
		if( C == 0 ) unite(A, B);
		else vec.push_back(make_pair(A, B));
	}
	int cnt = 0;
	for(int i=1;i<=N;i++)
		if( find(i) == i ) cnt++;
	ll P = N - cnt, mx = 1LL*(cnt - 1)*cnt/2 + P;
	if( mx < M ) {
		puts("No");
		return 0;
	}
	ll mn = (vec.empty() ? N - 1 : N);
	if( mn > M ) {
		puts("No");
		return 0;
	}
	for(int i=0;i<vec.size();i++)
		if( find(vec[i].first) == find(vec[i].second) ) {
			puts("No");
			return 0;
		}
	puts("Yes");
}