#define DEBUG
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 
using namespace std;
 
#define F first
#define S second 
#define pb push_back
#define endl "\n"

#define IOS { ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0); }

#ifdef DEBUG
    #define dbg(s) {s;}
#endif

#ifndef DEBUG
    #define dbg(s)
#endif

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int grand(int x) { // from [0, x-1]
    return uniform_int_distribution<int>(0, x - 1)(rng);
}

#define int ll
#define i32 int32_t

#define RBTTYPE int
#define ordered_set tree<RBTTYPE, null_type, less<RBTTYPE>, rb_tree_tag,tree_order_statistics_node_update> 

#define all(v) (v).begin(),(v).end()

typedef long long ll;
typedef long double ld;
typedef pair< int, int > pii;
typedef pair< ll, ll > pll;

int getInt () {
    bool minus = false;
    int result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' || ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus)
        return -result;
    else
        return result;
}


ll gcd(ll x, ll y) {
    if (x < y) return gcd(y, x);
    if (y == 0) return x;
    return gcd(y, x % y);
} 

const ll mod = 1e9 + 7;

ll modexp(ll x, ll ex) {
    ll ans = 1ll;
    while (ex > 0) {
        if (ex & 1ll) ans = (ans * x) % mod;
        ex >>= 1ll;
        x = (x * x) % mod;
    }
    return ans;
}

const int maxn = 1e5 + 7;

const ll inf = 1e9 + 7;

int ranki[maxn];
int parent[maxn];
vector<pair<ll,ll>> ones;

int find_dsu(ll x)
{
    if(parent[x]==-1)
        return x;
    ll par = parent[x];
    parent[x] = find_dsu(par);
    return parent[x];
}

int merge_dsu(ll x, ll y)
{
    x = find_dsu(x);
    y = find_dsu(y);
    
    if(x==y)
        return 0;
    if(ranki[x]>ranki[y])
    {
        parent[y] = x;
        ranki[x] += ranki[y];
    }
    else
    {
        parent[x] = y;
        ranki[y] += ranki[x];
    }
    return 1;
}


i32 main() {
    //freopen("<file>.in", "r", stdin);
    //freopen("<file>.out", "w", stdout);

    IOS;

   ll n,m,q;
  cin>>n>>m>>q;
 memset(parent,-1,sizeof(parent));
    for(ll i=0;i<n;i++)
       ranki[i] = 1;

       
   ll a,b,c;
    for(ll i=0;i<q;i++)
    {
        cin>>a>>b>>c;
        if(c==0)
        {
            merge_dsu(a,b);
        }
        else
        {
            ones.pb({a,b});
        }
    } 

    for(ll i=0;i<ones.size();i++)
    {
        a = find_dsu(ones[i].F);
        b = find_dsu(ones[i].S);
        if(a==b)
        {
            cout<<"No"<<endl;
            return 0;
        }
    }

    ll mini = n;
    if(ones.size()==0)
        mini--;
    if(m<mini)
    {
        cout<<"No"<<endl;
        return 0;
    }


    ll count=0;
    for(ll i=0;i<n;i++)
    {
        if(parent[i]==-1)
        {
            count++;
        }
    }
    ll maxi = n- count;
    maxi+= count*(count-1)/2;
    if(m>maxi)
    {
        cout<<"No"<<endl;
        return 0;
    }
    cout<<"Yes"<<endl;
}




Measured memory_bytes: 3956