#include using namespace std; using ll = long long; using edge = pair; void buildComp(map>& adj, vector& comp, ll color) { ll n = comp.size(); ll counter = 0; for (ll i = 0; i < n; ++i) { if (comp[i] != -1) continue; queue q{}; q.push(i); while (!q.empty()) { ll top = q.front(); q.pop(); comp[top] = counter; for (auto& [ to, c ] : adj[top]) { if (c != color) continue; if (comp[to] != -1) continue; q.push(to); } } ++counter; } } void process() { ll m, n; cin >> n >> m; map> adj{}; for (ll i = 0; i < m; ++i) { ll u, v, c; cin >> u >> v >> c; adj[u].emplace_back(v, c); adj[v].emplace_back(u, c); } vector comp_zero(n, -1); buildComp(adj, comp_zero, 0); vector comp_one(n, -1); buildComp(adj, comp_one, 1); for (auto& [ u, edge ] : adj) { for (auto& [ v, c ] : edge) { if (c == 0) { ll fc = comp_one[u]; ll sc = comp_one[v]; if (fc == -1 || sc == -1) { continue; } if (fc != sc) { continue; } cout << "NO\n"; return; } else { ll fc = comp_zero[u]; ll sc = comp_zero[v]; if (fc == -1 || sc == -1) { continue; } if (fc != sc) { continue; } cout << "NO\n"; return; } } } cout << "YES\n"; } int main() { std::ios::sync_with_stdio(false); process(); cout.flush(); return 0; }