#include using namespace std; using ll = long long; using vi = vector; using pi = pair ; using vvi = vector; void solve () { int n, m; cin >> n >> m; vvi gr[2] = {vvi(n), vvi(n)}; for (int i = 0; i < m; ++i) { int u, v, c; cin >> u >> v >> c; gr[c][u].push_back(v); gr[c][v].push_back(u); } vi comp[2] = {vi(n), vi(n)}; auto Dfs = [&](auto&& self, int v, int c, int tag) { if (comp[c][v] == tag) return; comp[c][v] = tag; for (int u : gr[c][v]) self(self, u, c, tag); }; for (int c = 0; c < 2; ++c) { int t = 1; for (int i = 0; i < n; ++i) { if (comp[c][i]) continue; Dfs(Dfs, i, c, t); ++t; } } bool good = true; for (int c = 0; c < 2 && good; ++c) { for (int v = 0; v < n && good; ++v) { for (int u : gr[c][v]) { assert(comp[1 - c][u] != 0); assert(comp[1 - c][v] != 0); if (comp[1 - c][u] == comp[1 - c][v]) { good = false; break; } } } } cout << (good ? "YES\n" : "NO\n"); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); solve(); return 0; }