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

const int MAXN = 100005;
int n, q;
long long m;
vector<pair<int, int> > con[2];
int p[MAXN];
int find(int i) {
	if (p[i] == -1 || p[i] == i) return i;
	return p[i] = find(p[i]);
}
bool un(int u, int v) {
	u = find(u);
	v = find(v);
	if (u == v) return false;
	p[u] = v;
	return true;
}
int main() {
	memset(p, -1, sizeof(p));
	scanf("%d %lld %d", &n, &m, &q);
	bool tree = true;
	for (int i = 0; i < q; i++) {
		int a, b, c;
		scanf("%d %d %d", &a, &b, &c);
		con[c].push_back({a, b});
		if (c) tree = false;
	}
	for (int i = 0; i < con[0].size(); i++) {
		un(con[0][i].first, con[0][i].second);
	}
	for (int i = 0; i < con[1].size(); i++) {
		if (find(con[1][i].first) == find(con[1][i].second)) {
			printf("No\n");
			return 0;
		}
	}
	int comp = 0;
	for (int i = 0; i < n; i++) if (find(i) == i) comp++;
	long long minM = n-1;
	if (!tree) {
		minM = n;
		if (comp <= 2) {
			printf("No\n");
			return 0;
		}
	}
	long long maxM = n-comp + (comp * 1ll * (comp - 1)) / 2;
	if (minM <= m && m <= maxM) {
		printf("Yes\n");
	} else printf("No\n");
}
