/*
 * @Author: RBQRBQ
 * @Date: 2020-04-17 00:12:29
 * @LastEditors: RBQRBQ
 * @LastEditTime: 2020-04-17 23:59:05
 */
/*
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
typedef vector<int> VI;
typedef pair<int,int> pii;
namespace IO{
    template<typename T>inline void read(T &x){
        x=0;ll f=1;char ch=getchar();
        while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
        while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
        x=x*f;
    }
}
using namespace IO;
const int maxn =200000;
int f[maxn+10];
int find(int x)
{
    if(f[x]==x) return x;
    else
    {
        f[x]=find(f[x]);
        return f[x];
    } 
}
int N;
ll M;
ll Q;
vector<pii> p1;
vector<pii> p2;
signed main()
{
    for(int i=0;i<=maxn;i++)
    f[i]=i;
    read(N),read(M),read(Q);
    ll pos=0;
    for(int i=1;i<=Q;i++)
    {
        int x,y,k;
        read(x),read(y),read(k);
        (k==0)?(p1.push_back(make_pair(x,y))):(p2.push_back(make_pair(x,y)));
        if(k==0){
        int f1=find(x);
        int f2=find(y);
        if(f1!=f2) {
        pos++;
        f[f1]=f2;}
        }
    }
    ll cnt=0;
    if(M==N-1)
    {
        if(p2.size()!=0){
        cout<<"No";return 0;}
        else {cout<<"Yes";return 0;}
    }
    else{
    for(int i=0;i<N;i++)
    if(find(i)==i) cnt++;
    for(auto p:p2)
    {
        if(find(p.second)==find(p.first))
        {
            cout<<"No";
            return 0;
        }
    }
    if(cnt<=2)
    {
        ll que=1ll*p2.size();
        ll ans=1ll;
        if(que!=0)
        {
            cout<<"No";
            return 0;
        }
        else
        {
            cout<<"Yes";
            return 0;
        }
    }
    else
    {
        ll que=1ll*p2.size();
        ll ans=(1ll*cnt*(cnt-1))/2;
        
        if(((M-1ll*pos)>ans)||((M-1ll*pos)<que))
        {
            cout<<"No";
            return 0;
        }
        else
        {
            cout<<"Yes";
            return 0;
        }
        
    }}
}
*/
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
int f[MAXN];
int F(int x) {
	if (f[x] == x) return x;
	else return f[x] = F(f[x]);
}
int x[MAXN], y[MAXN], type[MAXN];
int main() {
	int n, q; ll m;
    ll pos=0;
	read(n), read(m), read(q);
	for (int i = 1; i <= n; i++)
		f[i] = i;
	for (int i = 1; i <= q; i++) {
		read(x[i]), read(y[i]), read(type[i]);
		x[i]++, y[i]++;
		if (type[i] == 0) {
            if(F(x[i])!=F(y[i])){
			f[F(x[i])] = F(y[i]);pos++;}
		} else {
			if (m == n - 1) {
				puts("No");
				return 0;
			}
		}
	}
	for (int i = 1; i <= q; i++)
		if (type[i] && F(x[i]) == F(y[i])) {
			puts("No");
			return 0;
		}
	int cnt = 0;
	for (int i = 1; i <= n; i++)
		cnt += F(i) == i;
	if (cnt <= 2) {
		for (int i = 1; i <= q; i++)
			if (type[i]) {
				puts("No");
				return 0;
			}
	}
	ll ans = pos;
	ans += 1ll * cnt * (cnt - 1) / 2;
	if (ans >= m) puts("Yes");
	else puts("No");
	return 0;
}


Measured memory_bytes: 1792