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

const int maxn = 1e5+10;
typedef long long ll;
int fa[maxn];
int fnd(int x){
    return (fa[x]<0)?x:(fa[x]=fnd(fa[x]));
}
bool uni(int a,int b){
    a=fnd(a),b=fnd(b);
    if(a==b)    return false;
    if(fa[a]>fa[b]) swap(a,b);
    fa[a]+=fa[b];
    fa[b]=a;
    return true;
}
#define yes {cout<<"Yes"<<endl;return 0;}
#define no {cout<<"No"<<endl;return 0;}
int c[maxn],u[maxn],v[maxn];
int main(){
    ios::sync_with_stdio(false);
    memset(fa,-1,sizeof fa);
    int N,Q;    ll M;
    cin>>N>>M>>Q;
    for(int i=0;i<Q;++i)
        cin>>u[i]>>v[i]>>c[i];
    for(int i=0;i<Q;++i)
        if(c[i]==0)
            uni(u[i],v[i]);
    for(int i=0;i<Q;++i)
    {
        if(c[i]==1)
            if(fnd(u[i])==fnd(v[i]))
                no
    }
    int cnt=0;
    for(int i=0;i<Q;++i)
        if(c[i]==1)
            cnt++;
    ll cu=0;
    ll MAX=0,MIN=0;
    for(int i=0;i<N;++i)
        if(fa[i]<0)
            cu++;
    if(cnt==0)
        MIN=N-1;
    else MIN=N;
    MAX = N-cu+cu*(cu-1)/2;
    //cout<<MIN<<" "<<MAX<<endl;

    if(cu==1)
        if(M==N-1)  yes
        else no
    if(cu==2)
        if(cnt==0&&M==N-1)  yes
        else no

    if(M<=MAX&&M>=MIN)  yes
    no
}
