#include <bits/stdc++.h>
using namespace std;

typedef long long lint;

const int MAXN = 100010;

int U[MAXN], V[MAXN], C[MAXN];
int uni[MAXN], sz[MAXN];

int guni(int x) { return x == uni[x] ? x : uni[x] = guni(uni[x]); }
void unite(int x, int y) { uni[guni(x)] = guni(y); }

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int N, Q;
	lint M;

	cin >> N >> M >> Q;
	for(int i = 0; i < Q; i++) cin >> U[i] >> V[i] >> C[i];

	for(int i = 0; i < N; i++) uni[i] = i;
	for(int i = 0; i < Q; i++) if(C[i] == 0) unite(U[i], V[i]);
	for(int i = 0; i < Q; i++) if(C[i] == 1 && guni(U[i]) == guni(V[i])) {
		cout << "No";
		return 0;
	}
	int cnt = 0;
	for(int i = 0; i < N; i++) if(i == uni[i]) cnt++;
	if(M > lint(cnt) * (cnt - 1) / 2 + N - cnt) {
		cout << "No";
		return 0;
	}
	for(int i = 0; i < Q; i++) if(C[i] == 1 && (M == N - 1 || cnt == 2)) {
		cout << "No";
		return 0;
	}
	if(cnt == 2 && M != N - 1) {
		cout << "No";
		return 0;
	}
	cout << "Yes";
	return 0;
}
