Language: C++

.h>
using namespace std;
#define fr(i,low,up) for(register int i = low;i <= up; ++i)
#define nfr(i,low,up) for(register int i = low;i >= up; --i)
typedef long long ll;
const int N = 1e5+100;
int n,q;
ll m;

int fa[N];
inline int find(int x){
   return fa[x] == x ? x : fa[x] = find(fa[x]);
}

struct query{
   int u,v,c;
   bool operator < (const query &b) const {
      return c < b.c;
   }
}t[N];

int vis[N];
int bel[N];

int main(){
   scanf("%d%lld%d",&n,&m,&q);
   fr(i,1,q){
      scanf("%d%d%d",&t[i].u,&t[i].v,&t[i].c);
      ++t[i].u; ++t[i].v;
   }
   fr(i,1,n)fa[i] = i;
   sort(t+1,t+q+1);
   fr(i,1,q){
      if(t[i].c == 0){
         if(find(t[i].u) == find(t[i].v))continue ;
         fa[find(t[i].u)] = find(t[i].v);
      } else {
         if(find(t[i].u) == find(t[i].v)){
            puts("No"); return 0;
         }
         vis[find(t[i].u)] = vis[find(t[i].v)] = 1;
      }
   }
   int cnt = 0,cnt2 = 0;
   fr(i,1,n){
      if(!bel[find(i)])++cnt,bel[fa[i]] = 1;
      if(vis[i])++cnt2;
   }
   m -= n-cnt;
   if(cnt == 2 && cnt2 == 2){
      puts("No"); return 0;
   } else if(cnt2 == 2){
      cnt2 = 3;
   }
   if(m < cnt2){
      puts("No"); return 0;
   } else if(m > 1LL*cnt*(cnt-1)/2){
      puts("No"); return 0;
   }
   puts("Yes"); return 0;
}

Compiler output:
a.cc:1:1: error: expected unqualified-id before '.' token
    1 | .h>
      | ^
a.cc: In function 'int main()':
a.cc:26:4: error: 'scanf' was not declared in this scope
   26 |    scanf("%d%lld%d",&n,&m,&q);
      |    ^~~~~
a.cc:27:7: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   27 |    fr(i,1,q){
      |       ^
a.cc:3:39: note: in definition of macro 'fr'
    3 | #define fr(i,low,up) for(register int i = low;i <= up; ++i)
      |                                       ^
a.cc:31:7: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   31 |    fr(i,1,n)fa[i] = i;
      |       ^
a.cc:3:39: note: in definition of macro 'fr'
    3 | #define fr(i,low,up) for(register int i = low;i <= up; ++i)
      |                                       ^
a.cc:32:4: error: 'sort' was not declared in this scope; did you mean 'short'?
   32 |    sort(t+1,t+q+1);
      |    ^~~~
      |    short
a.cc:33:7: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   33 |    fr(i,1,q){
      |       ^
a.cc:3:39: note: in definition of macro 'fr'
    3 | #define fr(i,low,up) for(register int i = low;i <= up; ++i)
      |                                       ^
a.cc:39:13: error: 'puts' was not declared in this scope
   39 |             puts("No"); return 0;
      |             ^~~~
a.cc:45:7: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   45 |    fr(i,1,n){
      |       ^
a.cc:3:39: note: in definition of macro 'fr'
    3 | #define fr(i,low,up) for(register int i = low;i <= up; ++i)
      |                                       ^
a.cc:51:7: error: 'puts' was not declared in this scope
   51 |       puts("No"); return 0;
      |       ^~~~
a.cc:56:7: error: 'puts' was not declared in this scope
   56 |       puts("No"); return 0;
      |       ^~~~
a.cc:58:7: error: 'puts' was not declared in this scope
   58 |       puts("No"); return 0;
      |       ^~~~
a.cc:60:4: error: 'puts' was not declared in this scope
   60 |    puts("Yes"); return 0;
      |    ^~~~