Language: C++


#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <numeric>

template <class T, class U>
inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U>
inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

// [l, r) from l to r
struct range {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { ++i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr range(int l_, int r_): l(l_), r(std::max(l_, r_)) { }
  constexpr itr begin() const { return l; }
  constexpr itr end() const { return r; }
};

// [l, r) from r to l
struct revrange {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { --i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr revrange(int l_, int r_): l(l_ - 1), r(std::max(l_, r_) - 1) { }
  constexpr itr begin() const { return r; }
  constexpr itr end() const { return l; }
};


class union_find {
private:
  int component;
  std::vector<int> parent;

public:
  union_find() = default;
  union_find(int size_) { init(size_); }

  void init(int size_) {
    component = size_;
    parent.assign(size_, -1);
  }

  int count_components() const { 
    return component; 
  }
  int component_size(int i) { 
    return -parent[find_parent(i)]; 
  }
  bool same_component(int i, int j) { 
    return find_parent(i) == find_parent(j); 
  }

  int find_parent(int i) {
    if (parent[i] < 0) {
      return i;
    }
    else {
      return parent[i] = find_parent(parent[i]);
    }
  }

  bool unite(int i, int j) {
    i = find_parent(i);
    j = find_parent(j);
    if (i == j) {
      return false;
    }
    if (parent[i] > parent[j]) {
      std::swap(i, j);
    }
    parent[i] += parent[j];
    parent[j] = i;
    --component;
    return true;
  }

};

int main() {
  int N, Q;
  int64_t M;
  std::cin >> N >> M >> Q;
  std::vector<int> A(Q), B(Q), C(Q);
  union_find dsu(N);
  for (int i: range(0, Q)) {
    std::cin >> A[i] >> B[i] >> C[i];
  }
  std::vector<int> order(Q);
  std::iota(order.begin(), orderend(), 0);
  std::sort(order.begin(), orderend(), [&](int i, int j) {
    return C[i] < C[j];
  });
  for (int i: order) {
    if (C[i] == 0) {
      dsu.unite(A[i], B[i]);
    }
    else {
      if (dsu.same_component(A[i], B[i])) {
        std::cout << "No\n";
        return 0;
      }
    }
  }
  if (M == N - 1) {
    std::cout << (C[order.back()] == 0 ? "Yes" : "No") << '\n';
    return 0;
  }
  int64_t cmp = dsu.count_components();
  if (cmp <= 2) {
    std::cout << "No\n";
    return 0;
  }
  std::cout << (N - cmp + cmp * (cmp - 1) / 2 >= M ? "Yes" : "No") << '\n';
  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:117:28: error: 'orderend' was not declared in this scope; did you mean 'order'?
  117 |   std::iota(order.begin(), orderend(), 0);
      |                            ^~~~~~~~
      |                            order