#define STOPIT
#include <bits/stdc++.h>
#define loop(n) for (int ngtkana_is_genius = 0; ngtkana_is_genius < int(n); ngtkana_is_genius++)
#define rep(i, begin, end) for(int i = int(begin); i < int(end); i++)
#define all(v) v.begin(), v.end()
#define rand(l, r) std::uniform_int_distribution<>(l, r)(mt)
#define lint long long
auto cmn = [](auto& a, auto b){if (a > b) {a = b; return true;} return false;};
auto cmx = [](auto& a, auto b){if (a < b) {a = b; return true;} return false;};
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail){
  std::cerr << " " << head;
  debug_impl(tail...);
}
#ifndef STOPIT
#define debug(...)\
  std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\
  debug_impl(__VA_ARGS__);\
  std::cerr << std::noboolalpha;
#else
#define debug 0;
#endif

template < typename Container,
  typename Value = typename Container::value_type,
  std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr
>
std::istream& operator>> (std::istream& is, Container& v) {
  for (auto & x : v) is >> x;
  return is;
}

template < typename Container,
  typename Value = typename Container::value_type,
  std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr
>
std::ostream& operator<< (std::ostream& os, Container const& v) {
  os << "{";
  for (auto it = v.begin(); it != v.end(); it++)
    {os << (it != v.begin() ? "," : "") << *it;}
  return os << "}";
}

template <typename... Args, std::size_t ... Inds>
std::ostream& tuple_output_impl(
  std::ostream& os,
  const std::tuple<Args...>& tuple,
  std::integer_sequence<std::size_t, Inds...>
)
{
  os << "(";
  (void)std::initializer_list<int>{(
    (void)(os << (Inds > 0 ? "," : "") << std::get<Inds>(tuple)), 0
  )...};
  return os << ")";
}
template <typename... Args>
std::ostream& operator<< (std::ostream& os, const std::tuple<Args...>& tuple)
 { return tuple_output_impl(os, tuple, std::index_sequence_for<Args...>()); }

template <typename... Args, std::size_t ... Inds>
std::istream& tuple_input_impl(
  std::istream& is,
  std::tuple<Args...>& tuple,
  std::integer_sequence<std::size_t, Inds...>
)
{
  (void)std::initializer_list<int>{(
    (void)(is >> std::get<Inds>(tuple)), 0
  )...};
  return is;
}
template <typename... Args>
std::istream& operator>> (std::istream& is, std::tuple<Args...>& tuple)
  { return tuple_input_impl(is, tuple, std::index_sequence_for<Args...>()); }

class union_find
{
  int n;
  std::vector<int> prt;

public:
  union_find (int n) : n(n), prt(n, -1){}

  bool is_root (int x)        const {return prt.at(x) < 0;}

  int  size    (int x)        const {return -prt.at(find(x));}

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

  int  find    (int x)        const {
    while (!is_root(x)) x = prt.at(x);
    return x;
  }

  // Returns `true` if x and y are newly connected.
  // The smaller one x becomes a child of the larger one y.
  bool unite   (int x, int y) {
    if ((x = find(x)) == (y = find(y))) return false;
    if (size(x) > size(y)) std::swap(x, y);
    prt.at(y) += prt.at(x);
    prt.at(x) = y;
    return true;
  }
};

void err () {
  std::cout << "No" << std::endl;
  exit(0);
}

int main() {
  std::cin.tie(0); std::cin.sync_with_stdio(false);
  lint n, m, q;
  std::cin >> n >> m >> q;
  std::vector< std::tuple< int, int, int > > data(q);
  for (auto & tuple : data) {
    int a, b, c;
    std::cin >> a >> b >> c;
    tuple = std::make_tuple(c, a, b);
  }
  std::sort(all(data));
  debug(data);
  auto uf = union_find(n);
  auto qcnt = 0;
  for (auto tuple : data) {
    int a, b, c;
    std::tie(c, a, b) = tuple;
    if (c == 0) {
      uf.unite(a, b);
    } else {
      if (uf.same(a, b)) err();
      qcnt++;
    }
  }
  auto ccnt = 0;
  rep(i, 0, n)
    { ccnt += uf.is_root(i); }
  lint max = (lint)ccnt * (ccnt - 1) / 2 + n - ccnt;
  lint min = (lint)n - (qcnt == 0);
  debug(max);
  if ((lint)m < (lint)min || (lint)max < (lint)m)
    { err(); }
  std::cout << "Yes" << std::endl;
  return 0;
}


Measured memory_bytes: 1792