#include using namespace std; typedef long long ll; typedef pair pll; #define For(i, a, n) for(ll i=a;i void dbg(Args&&... args){ ((cerr<> n >> m; vec> neighz(n); vec> neigho(n); vec edgez; vec edgeo; for(ll i = 0; i < m; i++){ ll u,v,c; cin >> u >> v >> c; if(c == 0){ neighz[u].push_back(v); neighz[v].push_back(u); edgez.push_back({u,v}); }else{ neigho[u].push_back(v); neigho[v].push_back(u); edgeo.push_back({u,v}); } } vec compz(n,-1); vec compo(n,-1); for(ll i = 0; i < n; i++){ if(compz[i] != -1){ continue; } vec frontier = {i}; while(!frontier.empty()){ ll pos = frontier.back(); frontier.pop_back(); if(compz[pos] != -1){ continue; } compz[pos] = i; for(ll j = 0; j < neighz[pos].size(); j++){ frontier.push_back(neighz[pos][j]); } } } for(ll i = 0; i < n; i++){ if(compo[i] != -1){ continue; } vec frontier = {i}; while(!frontier.empty()){ ll pos = frontier.back(); frontier.pop_back(); if(compo[pos] != -1){ continue; } compo[pos] = i; for(ll j = 0; j < neigho[pos].size(); j++){ frontier.push_back(neigho[pos][j]); } } } for(ll i = 0; i < edgez.size(); i++){ if(compo[edgez[i].first] == compo[edgez[i].second]){ cout << "NO\n"; return; } } for(ll i = 0; i < edgeo.size(); i++){ if(compz[edgeo[i].first] == compz[edgeo[i].second]){ cout << "NO\n"; return; } } cout << "YES\n"; } int main(){ cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); ll t = 1;//cin>>t; while(t--)solve(); }