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

#define int long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n) for (int i = 1; i <= (n); i++)
#define all(x) (x).begin(), (x).end()
#define uniq(x) (x).erase(unique(all(x)), (x).end())
#define bit(n) (1LL << (n))
#define dump(x) cerr << #x " = " << (x) << endl
using vint=vector<int>;
using vvint=vector<vint>;
using pint=pair<int, int>;
using vpint=vector<pint>;
template<typename T> using priority_queue_rev=priority_queue<T, vector<T>, greater<T>>;
constexpr double pi = 3.1415926535897932384626433832795028;
constexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int gcd(int a, int b) {
    while (b) { swap(a %= b, b); }
    return a;
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
template<typename T1, typename T2> bool chmax(T1 &a, const T2 &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template<typename T1, typename T2> bool chmin(T1 &a, const T2 &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &rhs) {
    os << "(" << rhs.first << ", " << rhs.second << ")";
    return os;
}
template<typename T> ostream &operator<<(ostream &os, const vector<T> &rhs) {
    os << "{";
    for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
        os << *itr << (next(itr) != rhs.end() ? ", " : "");
    }
    os << "}";
    return os;
}
struct Setup {
    static constexpr int PREC = 20;
    Setup() {
        cout << fixed << setprecision(PREC);
        cerr << fixed << setprecision(PREC);
    };
} setup;
struct UnionFindTree {
    int n;
    std::vector<int> par, size;
    UnionFindTree(int n) : n(n), par(n), size(n) {
        for (int i = 0; i < n; i++) {
            par[i] = i;
            size[i] = 1;
        }
    }
    int getRoot(int i) {
        if (par[i] == i) { return i; }
        else { return par[i] = getRoot(par[i]); }
    }
    void unite(int i, int j) {
        if ((i = getRoot(i)) == (j = getRoot(j))) { return; }
        if (size[i] < size[j]) { std::swap(i, j); }
        par[j] = i;
        size[i] += size[j];
    }
    bool areSame(int i, int j) { return getRoot(i) == getRoot(j); }
    int getSize(int i) { return size[getRoot(i)]; }
};
int N, M, Q;
int A[110000], B[110000], C[110000];
pair<int, pint> q[110000];
signed main() {
    cin >> N >> M >> Q;
    rep(i, Q) {
        cin >> A[i] >> B[i] >> C[i];
        q[i] = {C[i], {A[i], B[i]}};
    }
    sort(q, q + Q);
    UnionFindTree uf(N);
    int k = N;
    rep (i, Q) {
        pint cur = q[i].second;
        if (q[i].first == 0) {
            if (!uf.areSame(cur.first, cur.second)) {
                uf.unite(cur.first, cur.second);
                k--;
            }
        } else {
            if (uf.areSame(cur.first, cur.second)) {
                cout << "No" << endl;
                return 0;
            }
        }
    }
    if ((k == 1 || k == 2) && M != N - 1) {
        cout << "No" << endl;
        return 0;
    }
    if (M == N - 1 && q[Q - 1].first == 1) {
        cout << "No" << endl;
        return 0;
    }
    if (k * (k - 1) / 2 + N - k < M) {
        cout << "No" << endl;
        return 0;
    }
    cout << "Yes" << endl;
}


Measured memory_bytes: 6784