//#define NDEBUG
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <iterator>
#include <vector>

namespace n91 {

  using i8 = std::int_fast8_t;
  using i32 = std::int_fast32_t;
  using i64 = std::int_fast64_t;
  using u8 = std::uint_fast8_t;
  using u32 = std::uint_fast32_t;
  using u64 = std::uint_fast64_t;
  using isize = std::ptrdiff_t;
  using usize = std::size_t;

  constexpr usize operator"" _z(unsigned long long x) noexcept {
    return static_cast<usize>(x);
  }

  template <class T> class integral_iterator {
  public:
    using difference_type = T;
    using value_type = T;
    using pointer = const value_type*;
    using reference = value_type;
    using iterator_category = std::random_access_iterator_tag;

  private:
    using self_type = integral_iterator<value_type>;
    value_type i;

  public:
    constexpr integral_iterator() noexcept : i() {}
    explicit constexpr integral_iterator(const value_type i) noexcept : i(i) {}
    constexpr self_type operator++(int) noexcept { return self_type(i++); }
    constexpr self_type operator--(int) noexcept { return self_type(i--); }
    constexpr self_type operator[](const difference_type rhs) const noexcept {
      return self_type(i + rhs);
    }
    constexpr self_type& operator++() noexcept {
      ++i;
      return *this;
    }
    constexpr self_type& operator--() noexcept {
      --i;
      return *this;
    }
    constexpr reference operator*() const noexcept { return i; }
    constexpr self_type operator+(const difference_type rhs) const noexcept {
      return self_type(i + rhs);
    }
    constexpr self_type operator-(const difference_type rhs) const noexcept {
      return self_type(i - rhs);
    }
    constexpr difference_type operator-(const self_type rhs) const noexcept {
      return i - rhs.i;
    }
    constexpr bool operator<(const self_type rhs) const noexcept {
      return i < rhs.i;
    }
    constexpr bool operator<=(const self_type rhs) const noexcept {
      return i <= rhs.i;
    }
    constexpr bool operator>(const self_type rhs) const noexcept {
      return i > rhs.i;
    }
    constexpr bool operator>=(const self_type rhs) const noexcept {
      return i >= rhs.i;
    }
    constexpr bool operator==(const self_type rhs) const noexcept {
      return i == rhs.i;
    }
    constexpr bool operator!=(const self_type rhs) const noexcept {
      return i != rhs.i;
    }
    constexpr self_type& operator+=(const difference_type rhs) noexcept {
      i += rhs;
      return *this;
    }
    constexpr self_type& operator-=(const difference_type rhs) noexcept {
      i -= rhs;
      return *this;
    }
  };
  template <class T>
  constexpr integral_iterator<T> make_int_itr(const T i) noexcept {
    return integral_iterator<T>(i);
  }
  class rep {
    const usize f, l;

  public:
    constexpr rep(const usize f, const usize l) noexcept : f(f), l(l) {}
    constexpr auto begin() const noexcept { return make_int_itr(f); }
    constexpr auto end() const noexcept { return make_int_itr(l); }
  };
  class revrep {
    const usize f, l;

  public:
    revrep(const usize f, const usize l) noexcept : f(l), l(f) {}
    auto begin() const noexcept {
      return std::make_reverse_iterator(make_int_itr(f));
    }
    auto end() const noexcept {
      return std::make_reverse_iterator(make_int_itr(l));
    }
  };
  template <class T> auto md_vec(const usize n, const T& value) {
    return std::vector<T>(n, value);
  }
  template <class... Args> auto md_vec(const usize n, Args... args) {
    return std::vector<decltype(md_vec(args...))>(n, md_vec(args...));
  }
  template <class T> constexpr T difference(const T& a, const T& b) {
    return a < b ? b - a : a - b;
  }
  template <class T> T scan() {
    T ret;
    std::cin >> ret;
    return ret;
  }

} // namespace n91
#include <cassert>
#include <iterator>
#include <utility>
#include <vector>

template <class Monoid> class segment_tree {
public:
  using value_structure = Monoid;
  using value_type = typename value_structure::value_type;
  using container_type = std::vector<value_type>;
  using const_reference = typename container_type::const_reference;
  using size_type = typename container_type::size_type;

protected:
  static size_type getsize(const size_type size) {
    size_type ret = 1;
    while (ret < size)
      ret <<= 1;
    return ret;
  }

  size_type size_;
  container_type tree;

  size_type base_size() const { return tree.size() >> 1; }
  void recalc(const size_type index) {
    tree[index] =
      value_structure::operation(tree[index << 1], tree[index << 1 | 1]);
  }

public:
  segment_tree() : size_(0), tree() {}
  explicit segment_tree(const size_type size)
    : size_(size), tree(getsize(size) << 1, value_structure::identity()) {}
  template <class InputIterator>
  segment_tree(InputIterator first, InputIterator last)
    : size_(::std::distance(first, last)), tree() {
    const size_type cap = getsize(size_);
    tree.reserve(cap << 1);
    tree.resize(cap, value_structure::identity());
    tree.insert(tree.end(), first, last);
    tree.resize(cap << 1, value_structure::identity());
    for (size_type i = cap - 1; i; --i)
      recalc(i);
  }

  bool empty() const { return !size_; }
  size_type size() const { return size_; }

  const_reference operator[](const size_type index) const {
    assert(index < size());
    return tree[index + base_size()];
  }
  value_type fold(size_type first, size_type last) const {
    assert(first <= last);
    assert(first <= size());
    assert(last <= size());
    value_type ret_l = value_structure::identity(),
      ret_r = value_structure::identity();
    for (first += base_size(), last += base_size(); first < last;
      first >>= 1, last >>= 1) {
      if (first & 1)
        ret_l = value_structure::operation(::std::move(ret_l), tree[first++]);
      if (last & 1)
        ret_r = value_structure::operation(tree[last - 1], ::std::move(ret_r));
    }
    return value_structure::operation(::std::move(ret_l), ::std::move(ret_r));
  }
  template <class F> size_type search(const F& f) const {
    if (f(value_structure::identity()))
      return 0;
    if (!f(tree[1]))
      return size() + 1;
    value_type acc = value_structure::identity();
    size_type i = 1;
    while (i < base_size())
      if (!f(value_structure::operation(acc, tree[i <<= 1])))
        acc = value_structure::operation(::std::move(acc), tree[i++]);
    return i - base_size() + 1;
  }

  template <class F> void update(size_type index, const F& f) {
    assert(index < size());
    index += base_size();
    tree[index] = f(::std::move(tree[index]));
    while (index >>= 1)
      recalc(index);
  }
};
#include <algorithm>
#include <limits>
template <class T> class min_monoid {
public:
  using value_type = T;
  static value_type operation(const value_type& x, const value_type& y) {
    return ::std::min(x, y);
  }
  static value_type identity() {
    return ::std::numeric_limits<value_type>::max();
  }
  static value_type reverse(const value_type& x) { return x; }
};
#include <algorithm>
#include <limits>
template <class T> class max_monoid {
public:
  using value_type = T;
  static value_type operation(const value_type& x, const value_type& y) {
    return ::std::max(x, y);
  }
  static value_type identity() {
    return ::std::numeric_limits<value_type>::lowest();
  }
  static value_type reverse(const value_type& x) { return x; }
};
#include <cassert>
#include <cstddef>
#include <utility>
#include <vector>

namespace n91 {

  class incremental_connectivity {
  protected:
    class node_type;

  public:
    using container_type = std::vector<node_type>;
    using size_type = typename container_type::size_type;
    class connected_component {
      friend incremental_connectivity;
      const node_type& root;
      constexpr connected_component(const node_type& root) noexcept
        : root(root) {}

    public:
      constexpr size_type representative() const noexcept { return root.parent; }
      constexpr size_type size() const noexcept { return root.size; }
      constexpr bool operator==(const connected_component& rhs) const noexcept {
        return &root == &rhs.root;
      }
      constexpr bool operator!=(const connected_component& rhs) const noexcept {
        return &root != &rhs.root;
      }
    };

  protected:
    class node_type {
    public:
      size_type parent, size;
    };
    container_type tree;

  public:
    incremental_connectivity() : tree() {}
    explicit incremental_connectivity(const size_type size) : tree(size, { 0, 1 }) {
      for (size_type i = 0; i < size; ++i)
        tree[i].parent = i;
    }

    bool empty() const { return tree.empty(); }
    size_type size() const { return tree.size(); }

    connected_component find_cc(size_type x) {
      while (tree[x].parent != x) {
        x = tree[x].parent = tree[tree[x].parent].parent;
      }
      return connected_component(tree[x]);
    }

    std::pair<size_type, size_type> unite(size_type x, size_type y) {
      assert(x < size());
      assert(y < size());
      x = find_cc(x).representative();
      y = find_cc(y).representative();
      if (x != y) {
        if (tree[x].size < tree[y].size)
          std::swap(x, y);
        tree[x].size += tree[y].size;
        tree[y].parent = x;
      }
      return { x, y };
    }
  };

} // namespace n91

#include <algorithm>
#include <iostream>
#include <set>
#include <utility>

namespace n91 {

  bool main_() {
    const usize n = scan<usize>();
    const u64 m = scan<u64>();
    const usize q = scan<usize>();
    incremental_connectivity uf(n);
    struct edge {
      usize a, b;
    };
    std::vector<edge> one, two;
    for (const auto i : rep(0_z, q)) {
      const usize a = scan<usize>();
      const usize b = scan<usize>();
      const u32 c = scan<u32>();
      if (c == static_cast<u32>(0)) {
        one.push_back({ a, b });
      }
      else {
        two.push_back({ a, b });
      }
    }
    for (const auto& e : one) {
      uf.unite(e.a, e.b);
    }
    for (const auto& e : two) {
      if (uf.find_cc(e.a) == uf.find_cc(e.b)) {
        return false;
      }
    }
    u64 cnt = 0;
    for (const auto i : rep(0_z, n)) {
      if (uf.find_cc(i).representative() == i) {
        ++cnt;
      }
    }
    u64 min = n;
    if (two.empty()) {
      --min;
    }
    u64 max = n + (cnt - 1) * cnt / 2 - cnt;
    if (min <= m && m <= max) {
      return true;
    }
    return false;
  }

} // namespace n91

int main() {
  std::cout << (n91::main_() ? "Yes" : "No") << std::endl;
  return 0;
}


Measured memory_bytes: 4596