#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7, mod = 998244353;
#define debug(x) cerr << #x << " " << x << "\n";
#define int long long
int rep[N];
vector <pair<int, int> > kra;

void no() {
	cout << "No\n";
	exit(0);
}

void yes() {
	cout << "Yes\n";
	exit(0);
}

int re(int x) {
	if(rep[x] != x) rep[x] = re(rep[x]);
	return rep[x];
}

void uni(int a, int b) {
	rep[re(a)] = re(b);
}

int32_t main() {
	ios_base::sync_with_stdio(0);
	cin.tie();
	long long n, m, q;
	cin >> n >> m >> q;
	for(int i = 1; i <= n; i++) rep[i] = i;

	for(int i = 0; i < q; i++) {
		int a, b, c;
		cin >> a >> b >> c;
		a++;
		b++;
		if(c == 0) {
			uni(a, b);
		} else {
			kra.push_back(make_pair(a, b));
		}
	}

	if(m == n - 1) {
		if(kra.size() == 0) yes();
		else no();
	}

	for(auto x : kra) {
		if(re(x.first) == re(x.second)) no();
	}

	set <int> S;
	for(int i = 1; i <= n; i++) S.insert(re(i));
	long long k = S.size();

	if(m <= n - k + ((k * (k - 1)) / 2)) yes();
	no();
}