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 (Clang 3.8.0) (judge verdict: Runtime Error)

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cstdint>
using namespace std;
typedef long long ll;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define mod 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define int long long
class UnionFind {
private:
public:
    int sz;
    vector<int> par, size_,psz;
    vector<bool> flag;
    UnionFind(){}
    UnionFind(int node_size) : sz(node_size), par(sz), size_(sz, 1),psz(sz,1),flag(sz,0){
        iota(par.begin(), par.end(), 0);
    }
    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(size_[x] < size_[y]) swap(x,y);
        par[y] = x;
        size_[x] += size_[y];
    }
    void unite2(int x,int y){
        x = find(x), y = find(y);
        if(x == y) return;
        if(size_[x] < size_[y]) swap(x,y);
        par[y] = x;
        size_[x] += size_[y];
        psz[x] +=psz[y];
    }
    void unite3(int x,int y){
        x = find(x), y = find(y);
        par[y] = x;
        size_[x] += size_[y];
        psz[x] +=psz[y];    
    }
    int size(int x){
        x = find(x);
        return size_[x];
    }
    int psize(int x){
        x = find(x);
        return psz[x];
    }
    bool same(int x,int y){
        return find(x) == find(y);
    }
};


signed main(){
    long long n,m,q;
    cin >> n >> m >> q;
    ll K = 0;
    UnionFind uf(n);
    vector<pair<int,int> > p;
    rep(i,q){
        int a,b,c;
        cin >> a >> b >> c;
        if(c==1){
            p.push_back(MP(a,b));
        }else{
            if(uf.same(a,b)){
            }else{
                uf.unite(a,b);
                K++;
            }
        }
    }
    bool flag[100001]={};
    int c = 0;
    rep(i,n){
        if(!flag[uf.find(i)]){
            c++;
            flag[uf.find(i)]=1;
        }
    }
    if(c<=2&&p.size()!=0){
        //assert(0);
        cout << "No" << endl;
        return 0;
    }
    vector<ll> pp(n);
    UnionFind uf2(n);
    vector<int> pppppp(n);
    rep(i,n){
        pppppp[i] = uf.find(i);
    }
    rep(i,p.size()){
        int a = p[i].first;
        int b = p[i].second;
        if(pppppp[a]==pppppp[b]){
            //assert(0);
            cout << "No" << endl;
            return 0;
        }else{
            // if(c<=2){
            //     cout <<"No" << endl;
            //     return 0;
            // }
            uf.unite(a,b);
            uf2.unite2(a,b);
            uf2.flag[uf2.find(a)] = true;
            c--;
        }
    }
    vector<ll> sp(n);
    rep(i,n){
        if(uf2.flag[uf2.find(i)])sp[uf2.find(i)] = max(uf2.psize(i),sp[uf2.find(i)]);
        if(uf.size(i)==1){ 
            sp[i] = 1;
        }
    }
    priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>> >qqq;
    multiset<int> st;
    rep(i,n){
        //cerr << i << " " << sp[i] << endl;
        
        if(sp[i]!=0){
            if(sp[i]!=2)qqq.push(MP(sp[i],uf.find(i)));
            else st.insert(uf.find(i));
        }
    }
    vector<pair<ll,ll> >s;
    while(!qqq.empty()){
        auto x = qqq.top();
        qqq.pop();
        if(st.size()==0){
            s.push_back(MP(x.first,x.second));
            continue;    
        }
        int p = *st.begin();
        if(p!=uf.find(x.second)){
            uf.unite3(p,uf.find(x.second));
            qqq.push(MP(x.first+2,p));
            st.erase(st.find(p));
        }else{
            auto q = st.upper_bound(uf.find(x.second));
            if(q==st.end()){
                s.push_back(MP(x.first,x.second));
            }else{
                uf.unite3(*q,uf.find(x.second));
                qqq.push(MP(x.first+2,*q));
                st.erase(q);
            }
        }
    }
    if(st.size()!=0){
        //assert(0);
        cout << "No" << endl;
        return 0;
    }
    ll sss = 0;
    ll xxx = 0;
    ll ppp = 0;
    vector<ll> sd(n);
    rep(i,s.size()){
        if(s[i].first!=1)xxx += s[i].first;
        sss += s[i].first*(s[i].first-1)/2;
        sd[uf.find(s[i].second)] = max(s[i].first,sd[uf.find(s[i].second)]);
    }
    rep(i,n){
        sss -= sd[i]*(sd[i]-1)/2;
        ppp += sd[i];
    }
    //cerr << sss << " " << K << " " << m << endl;
    //assert(0);
    cerr << K << " " << xxx << " " << sss << " " << ppp << endl;
    if(K+sss+ppp*(ppp-1)/2 >=m && K+xxx <=m){
        
        cout << "Yes" << endl;
    }else{
        assert(0);
        cout << "No" << endl;
    }
    return 0;
}