Problem:
Score : 700 points

Problem Statement

Snuke's mother gave Snuke an undirected graph consisting of N vertices numbered 0 to N-1 and M edges.
This graph was connected and contained no parallel edges or self-loops.

One day, Snuke broke this graph.
Fortunately, he remembered Q clues about the graph.
The i-th clue (0 \leq i \leq Q-1) is represented as integers A_i,B_i,C_i and means the following:

If C_i=0: there was exactly one simple path (a path that never visits the same vertex twice) from Vertex A_i to B_i.

If C_i=1: there were two or more simple paths from Vertex A_i to B_i.

Snuke is not sure if his memory is correct, and worried whether there is a graph that matches these Q clues.
Determine if there exists a graph that matches Snuke's memory.

Constraints

2 \leq N \leq 10^5

N-1 \leq M \leq N \times (N-1)/2

1 \leq Q \leq 10^5

0 \leq A_i,B_i \leq N-1

A_i \neq B_i

0 \leq C_i \leq 1

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M Q
A_0 B_0 C_0
A_1 B_1 C_1
\vdots
A_{Q-1} B_{Q-1} C_{Q-1}

Output

If there exists a graph that matches Snuke's memory, print Yes; otherwise, print No.

Sample Input 1

5 5 3
0 1 0
1 2 1
2 3 0

Sample Output 1

Yes

For example, consider a graph with edges (0,1),(1,2),(1,4),(2,3),(2,4). This graph matches the clues.

Sample Input 2

4 4 3
0 1 0
1 2 1
2 3 0

Sample Output 2

No

Sample Input 3

10 9 9
7 6 0
4 5 1
9 7 0
2 9 0
2 3 0
4 1 0
8 0 0
9 1 0
3 0 0

Sample Output 3

No

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef pair<ll, ll> LP;
typedef vector<ll> vec;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-5;
const ld pi = acos(-1.0);

struct uf {
private:
	vector<int> par, ran;
	vector<int> cnt;
public:
	uf(int n) {
		par.resize(n, 0);
		ran.resize(n, 0);
		cnt.resize(n, 1);
		rep(i, n) {
			par[i] = i;
		}
	}
	int find(int x) {
		if (par[x] == x)return x;
		else return par[x] = find(par[x]);
	}
	void unite(int x, int y) {
		x = find(x), y = find(y);
		if (x == y)return;
		if (ran[x] < ran[y]) {
			par[x] = y; cnt[y] += cnt[x];
		}
		else {
			par[y] = x; cnt[x] += cnt[y];
			if (ran[x] == ran[y])ran[x]++;
		}
	}
	bool same(int x, int y) {
		return find(x) == find(y);
	}
	int comp(int x) {
		return cnt[find(x)];
	}
};

vector<int> G[1 << 17];

void solve() {
	int n; ll m; int  q; cin >> n >> m >> q;
	uf u(n);
	vector<P> v;
	int num = 0; ll ma = 0;
	rep(i, q) {
		int a, b, c; cin >> a >> b >> c;
		if (c == 0) {
			/*if (u.same(a, b)) {
				cout << "No" << endl; return;
			}*/
			if (!u.same(a, b)) {
				num++; ma++;
			}
			u.unite(a, b);
		}
		else {
			v.push_back({ a,b });
		}
	}
	map<P, bool> used;
	rep(i, v.size()) {
		int l = v[i].first, r = v[i].second;
		l = u.find(l), r = u.find(r);
		if (l > r)swap(l, r);
		if (l==r) {
			cout << "No" << endl; return;
		}
		if (used[{l, r}])continue;
		G[l].push_back(r); G[r].push_back(l);
		used[{l, r}] = true;
	}
	vector<bool> chked(n, false);

	ll dr = 0;
	vector<ll> z;
	int ad = 0;
	bool exi = false;
	rep(i, n) {
		int p = u.find(i);
		if (chked[p])continue;
		queue<int> q; q.push(p); chked[p] = true;
		vector<int> ids;
		while (!q.empty()) {
			int id = q.front(); q.pop();
			ids.push_back(id);
			rep(j, G[id].size()) {
				int to = G[id][j];
				if (chked[to])continue;
				chked[to] = true;
				q.push(to);
			}
		}
		int edges = 0;
		rep(j, ids.size()) {
			int id = ids[j];
			edges += G[id].size();
		}
		edges /= 2;
		ll len = ids.size();
		if(len>1)num += len;
		dr += len;
		z.push_back(len);
	}
	if (z == vector<ll>{2}) {
		cout << "No" << endl; return;
	}
	//cout << num << " " << ma << endl;
	ma += dr * (dr - 1) / 2;
	if (m < num || ma < m) {
		cout << "No" << endl; return;
	}
	cout << "Yes" << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//init();
	solve();
	//stop
	return 0;
}