Language: C++

#include <iostream>
#include <algorithm>
#include <map>
#include <set>

const int64_t MAX = 100005;

int64_t N[MAX];
int64_t L[MAX];
struct Query
{
    int64_t a,b,c;
    bool operator<(const Query& q) const
    {
        return c > q.c;
    }
} Q[MAX];
int64_t n,m,q;

int64_t Find(int64_t a)
{
    if (N[a] == a) return a;
    else return N[a] = Find(N[a]);
}

void Union(int64_t l, int64_t r)
{
    if (L[l] < L[r]) Union(r, l);
    else {
        ++L[l];
        N[r] = l;
    }
}

int64_t main()
{
    std::ios_base::sync_with_stdio(0);
    std::cin >> n >> m >> q;
    std::set<int64_t> U;
    for (int64_t i=0;i<n;++i) N[i] = i;
    for (int64_t i=0;i<q;++i) std::cin >> Q[i].a >> Q[i].b >> Q[i].c;
    std::sort(Q, Q+q);
    bool result = true;
    for (int64_t i=0;i<q;++i)
    {
        if (Q[i].c) Union(Find(Q[i].a), Find(Q[i].b));
        else
        {
            result &= Find(Q[i].a) != Find(Q[i].b);
        }
        U.insert(Q[i].a);
        U.insert(Q[i].b);
    }
    int64_t nn = n - U.size();
    int64_t ml = n-1;
    int64_t mr = n-1;
    std::map<int64_t, int64_t> C;
    for (int64_t i=0;i<n;++i) C[Find(i)]++;
    int64_t maxm = 0;
    for (auto it : C)
    {
        if (it.second > 2)
        {
            maxm = std::max(maxm, it.second);
            ++ml;
            mr += it.second*(it.second-1)/2 - it.second+1;
        }
        else if (it.second == 2)
        {
            if (nn <= 0) result = false;
            --nn;
            
            maxm = std::max(maxm, 3);
            ++ml;
            mr += 1;
        }
    }
    mr -= maxm*(maxm-1)/2 - maxm+1;
    maxm += nn;
    mr += maxm*(maxm-1)/2 - maxm+1;
    result &= ml <= m && m <= mr;
    if (ml == n-1 && m > n-1) result = false;
    std::cout << (result ? "Yes" : "No") << std::endl;

    return 0;
}

Compiler output:
a.cc:35:1: error: '::main' must return 'int'
   35 | int64_t main()
      | ^~~~~~~
a.cc: In function 'int main()':
a.cc:73:28: error: no matching function for call to 'max(int64_t&, int)'
   73 |             maxm = std::max(maxm, 3);
      |                    ~~~~~~~~^~~~~~~~~
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:73:28: note:   deduced conflicting types for parameter 'const _Tp' ('long int' and 'int')
   73 |             maxm = std::max(maxm, 3);
      |                    ~~~~~~~~^~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:73:28: note:   mismatched types 'std::initializer_list<_Tp>' and 'long int'
   73 |             maxm = std::max(maxm, 3);
      |                    ~~~~~~~~^~~~~~~~~