#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5;
struct Element
{
    int u,v,opt;
} p[N+3];
int fa[N+3];
ll n,q,c,m;

int find(int u) {return fa[u]==u?u:fa[u]=find(fa[u]);}

int main()
{
    cin>>n>>m>>q;
    for(int i=1; i<=n; i++) fa[i] = i;
    for(int i=1; i<=q; i++)
    {
        scanf("%d%d%d",&p[i].u,&p[i].v,&p[i].opt); p[i].u++; p[i].v++;
        if(p[i].opt==0)
        {
            int uu = find(p[i].u),vv = find(p[i].v);
            if(uu!=vv) {fa[uu] = vv;}
        }
    }
    ll huan = 0; c = 0;
    
    for(int i=1; i<=n; i++) {if(i==find(i)) c++;}
    for(int i=1;i<=q;i++){
        if(p[i].opt==1){
            huan++;
            if(find(p[i].u)==find(p[i].v)){
                printf("No\n");
                return 0;
            }
        }
    }
    
    ll temp=m-(n-c);
    ll l,r;
    l = huan?max(3ll,(ll)c):c-1ll,r = 1ll*c*(c-1ll)/2ll;
    if(l<=temp&&temp<=r)
        printf("Yes\n");
    else printf("No\n");
}
