#include <bits/stdc++.h>
#define ALL(v) std::begin(v), std::end(v)
#define ALLR(v) std::rbegin(v), std::rend(v)
using ll = std::int64_t;
using ull = std::uint64_t;
using pii = std::pair<int, int>;
using tii = std::tuple<int, int, int>;
using pll = std::pair<ll, ll>;
using tll = std::tuple<ll, ll, ll>;
template <typename T> using vec = std::vector<T>;
template <typename T> using vvec = vec<vec<T>>;
template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename T, typename... Tail> const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); }
template <typename T, typename... Tail> const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }
template <typename T> const T& clamp(const T &t, const T &low, const T &high) { return std::max(low, std::min(high, t)); }
template <typename T> void chclamp(T &t, const T &low, const T &high) { return t = clamp(t, low, high); }
template <typename T> T make_v(T init) { return init; }
template <typename T, typename... Tail> auto make_v(T init, std::size_t s, Tail... tail) { auto v = std::move(make_v(init, tail...)); return vec<decltype(v)>(s, v); }
template <typename T, std::size_t Head, std::size_t ...Tail> struct multi_dem_array { using type = std::array<typename multi_dem_array<T, Tail...>::type, Head>; };
template <typename T, std::size_t Head> struct multi_dem_array<T, Head> { using type = std::array<T, Head>; };
template <typename T, std::size_t ...Args> using mdarray = typename multi_dem_array<T, Args...>::type;
namespace init__ { struct InitIO { InitIO() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(30); } } init_io; }

namespace tree {

struct UnionFind {
    vec<ll> rank, par;
    vec<ssize_t> sz;

    UnionFind(ll n) : rank(n, 1), par(n), sz(n, 1) {
        iota(ALL(par), 0);
    }

    ll find(ll n) {
        return (n == par[n] ? n : par[n] = find(par[n]));
    }

    bool unit(ll x, ll y) {
        ll px = find(x), py = find(y);
        if (px == py) return false;
        if (rank[px] < rank[py]) std::swap(px, py);
        par[py] = px;
        rank[px] += (rank[px] == rank[py]);
        sz[px] += sz[py];
        return true;
    }

    bool same(ll x, ll y) {
        return find(x) == find(y);
    }

    ssize_t size(ll n) {
        return sz[find(n)];
    }
};

}

bool solve() {
    ll n, m, q;
    std::cin >> n >> m >> q;
    tree::UnionFind uf(n);
    vec<pll> q1, q2;
    while (q--) {
        ll a, b, c;
        std::cin >> a >> b >> c;
        (c == 0 ? q1 : q2).emplace_back(a, b);
    }

    ll cnt = 0;
    for (auto &&e : q1) {
        ll a, b;
        std::tie(a, b) = e;
        cnt += uf.unit(a, b);
    }

    vec<bool> used(n);
    for (auto &&e : q2) {
        ll a, b;
        std::tie(a, b) = e;
        if (uf.same(a, b)) return false;
        used[uf.find(a)] = true;
        used[uf.find(b)] = true;
    }

    auto get_pars = [&] {
        std::set<ll> pars;
        for (ll i = 0; i < n; i++) if (uf.find(i) == i) pars.insert(i);
        return pars;
    };

    vec<ll> cands;

    ll sz = get_pars().size();
    if (sz != 2) cands.push_back(cnt + sz * (sz - 1) / 2);

    ll node = -1;
    for (ll i = 0; i < n; i++) {
        ll p = uf.find(i);
        if (used[p]) continue;
        if (node == -1) node = i;
        else cnt += uf.unit(node, i);
    }

    sz = get_pars().size();
    if (sz != 2) cands.push_back(cnt + (sz == 1 ? 0 : sz));

    if (node != -1) {
        ll p = uf.find(node);
        for (ll i = 0; i < n; i++) if (uf.find(i) != p) {
            cnt += uf.unit(p, i);
            break;
        }
    }

    sz = get_pars().size();
    if (sz != 2) cands.push_back(cnt + (sz == 1 ? 0 : sz));

    sort(ALL(cands));
    return cands.size() && cands.front() <= m && m <= cands.back();
}

int main() {
    std::cout << (solve() ? "Yes" : "No") << '\n';
    return 0;
}


Measured memory_bytes: 8816