Language: C++

#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define PII pair < int , int >
const int _ = 1e5 + 7;
ll fa[_] , sz[_] , N , M , Q;
vector < PII > to0 , to1; vector < int > ch[_];

int find(int x){return fa[x] == x ? x : (fa[x] = find(fa[x]));}
void merge(int p , int q){
	if((p = find(p)) == (q = find(q))) return;
	fa[p] = q; sz[q] += sz[p];
}

bool vis[_] , vis1[_];
bool dfs(int x){
	if(vis1[find(x)]) return 1;
	vis[x] = 1; vis1[find(x)] = 1;
	for(auto t : ch[x]) if(!vis[t] && dfs(t)) return 1;
	return 0;
}

void clr(int x){
	vis1[find(x)] = 0;
	for(auto t : ch[x]) if(vis1[t]) clr(t);
}

signed main()
	ios::sync_with_stdio(0);
	cin >> N >> M >> Q;
	for(int i = 1 ; i <= N ; ++i) sz[fa[i] = i] = 1;
	for(int i = 1 ; i <= Q ; ++i){
		int A , B , C; cin >> A >> B >> C; ++A; ++B;
		if(C == 0) to0.push_back(PII(A , B));
		else to1.push_back(PII(A , B));
	}
	for(auto t : to0) merge(t.first , t.second);
	for(auto t : to1){
		ch[t.first].push_back(t.second); ch[t.second].push_back(t.first);
	}
	for(int i = 1 ; i <= N ; ++i)
		if(!vis[i]){
			if(dfs(i)) {puts("No"); return 0;}
			clr(i);
		}
	ll mn = 0 , mx = 0 , cnt = 0; int flg = 0;
	for(int i = 1 ; i <= N ; ++i)
		if(find(i) == i){++cnt; mn += sz[i] - 1; mx += sz[i] - 1;}
	for(int i = 1 ; i <= N ; ++i)
		if(fa[i] == i) sz[i] = 1; else sz[i] = 0;
	for(auto t : to1) merge(t.first , t.second);
	for(int i = 1 ; i <= N ; ++i)
		if(fa[i] == i)
			if(sz[i] > 2){flg += 100; mn += sz[i];}
			else if(sz[i] == 2){
				if(cnt == 2){puts("No"); return 0;}
				else mn += 2;
				++flg;
			}
	if(flg == 1) ++mn;
	if(M < mn){puts("No"); return 0;}
	mx += cnt * (cnt - 1) / 2;
	if(M > mx){puts("No"); return 0;}
	puts("Yes"); return 0;
}


Compiler output:
a.cc:30:9: error: expected initializer before 'ios'
   30 |         ios::sync_with_stdio(0);
      |         ^~~
a.cc:31:9: error: 'cin' does not name a type
   31 |         cin >> N >> M >> Q;
      |         ^~~
a.cc:32:9: error: expected unqualified-id before 'for'
   32 |         for(int i = 1 ; i <= N ; ++i) sz[fa[i] = i] = 1;
      |         ^~~
a.cc:32:25: error: 'i' does not name a type
   32 |         for(int i = 1 ; i <= N ; ++i) sz[fa[i] = i] = 1;
      |                         ^
a.cc:32:34: error: expected unqualified-id before '++' token
   32 |         for(int i = 1 ; i <= N ; ++i) sz[fa[i] = i] = 1;
      |                                  ^~
a.cc:33:9: error: expected unqualified-id before 'for'
   33 |         for(int i = 1 ; i <= Q ; ++i){
      |         ^~~
a.cc:33:25: error: 'i' does not name a type
   33 |         for(int i = 1 ; i <= Q ; ++i){
      |                         ^
a.cc:33:34: error: expected unqualified-id before '++' token
   33 |         for(int i = 1 ; i <= Q ; ++i){
      |                                  ^~
a.cc:38:9: error: expected unqualified-id before 'for'
   38 |         for(auto t : to0) merge(t.first , t.second);
      |         ^~~
a.cc:39:9: error: expected unqualified-id before 'for'
   39 |         for(auto t : to1){
      |         ^~~
a.cc:42:9: error: expected unqualified-id before 'for'
   42 |         for(int i = 1 ; i <= N ; ++i)
      |         ^~~
a.cc:42:25: error: 'i' does not name a type
   42 |         for(int i = 1 ; i <= N ; ++i)
      |                         ^
a.cc:42:34: error: expected unqualified-id before '++' token
   42 |         for(int i = 1 ; i <= N ; ++i)
      |                                  ^~
a.cc:48:9: error: expected unqualified-id before 'for'
   48 |         for(int i = 1 ; i <= N ; ++i)
      |         ^~~
a.cc:48:25: error: 'i' does not name a type
   48 |         for(int i = 1 ; i <= N ; ++i)
      |                         ^
a.cc:48:34: error: expected unqualified-id before '++' token
   48 |         for(int i = 1 ; i <= N ; ++i)
      |                                  ^~
a.cc:50:9: error: expected unqualified-id before 'for'
   50 |         for(int i = 1 ; i <= N ; ++i)
      |         ^~~
a.cc:50:25: error: 'i' does not name a type
   50 |         for(int i = 1 ; i <= N ; ++i)
      |                         ^
a.cc:50:34: error: expected unqualified-id before '++' token
   50 |         for(int i = 1 ; i <= N ; ++i)
      |                                  ^~
a.cc:51:43: error: expected unqualified-id before 'else'
   51 |                 if(fa[i] == i) sz[i] = 1; else sz[i] = 0;
      |                                           ^~~~
a.cc:52:9: error: expected unqualified-id before 'for'
   52 |         for(auto t : to1) merge(t.first , t.second);
      |         ^~~
a.cc:53:9: error: expected unqualified-id before 'for'
   53 |         for(int i = 1 ; i <= N ; ++i)
      |         ^~~
a.cc:53:25: error: 'i' does not name a type
   53 |         for(int i = 1 ; i <= N ; ++i)
      |                         ^
a.cc:53:34: error: expected unqualified-id before '++' token
   53 |         for(int i = 1 ; i <= N ; ++i)
      |                                  ^~
a.cc:56:25: error: expected unqualified-id before 'else'
   56 |                         else if(sz[i] == 2){
      |                         ^~~~
a.cc:61:9: error: expected unqualified-id before 'if'
   61 |         if(flg == 1) ++mn;
      |         ^~
a.cc:62:9: error: expected unqualified-id before 'if'
   62 |         if(M < mn){puts("No"); return 0;}
      |         ^~
a.cc:63:9: error: 'mx' does not name a type
   63 |         mx += cnt * (cnt - 1) / 2;
      |         ^~
a.cc:64:9: error: expected unqualified-id before 'if'
   64 |         if(M > mx){puts("No"); return 0;}
      |         ^~
a.cc:65:13: error: expected constructor, destructor, or type conversion before '(' token
   65 |         puts("Yes"); return 0;
      |             ^
a.cc:65:22: error: expected unqualified-id before 'return'
   65 |         puts("Yes"); return 0;
      |                      ^~~~~~
a.cc:66:1: error: expected declaration before '}' token
   66 | }
      | ^