#include using namespace std; typedef long long ll; template ostream& operator<< (ostream& os, const vector & v) { os << "[ "; for (const T & elem : v) { os < elem << " "; } os << " ]"; return os; } bool bfs(pair v, ll main_color, vector & pred, vector & visited, const vector>> & graph) { queue> q; q.push(v); bool inSecondFloor = false; while (!q.empty()) { ll curr_v = q.front().first; ll curr_floor = q.front().second; //cout << "curr_v: " << curr_v << " curr_floor: " << curr_floor << " " << inSecondFloor <> n >> m; vector>> graph(n); for (ll i = 0; i < m; ++i) { ll u, v, c; cin >> u >> v >> c; graph[u].push_back({v, c}); graph[v].push_back({u, c}); } // RED - 0 // BLUE - 1 vector pred(n, -1); // pred[v][copy] vector visited(n, false); bool has_red_cycle_with_one_blue = false; for (ll i = 0; i < n; ++i) { if (!visited[i]) { pred[i] = i; visited[i] = true; has_red_cycle_with_one_blue |= bfs({i, 0}, 0, pred, visited, graph); } } //if(has_red_cycle_with_one_blue) // cout << "true" << endl; //else // cout << "false" << endl; //cout << "------" << endl; pred = vector (n, -1); visited = vector(n, false); bool has_blue_cycle_with_one_red = false; for (ll i = 0; i < n; ++i) { if (!visited[i]) { pred[i] = i; visited[i] = true; has_blue_cycle_with_one_red |= bfs({i, 0}, 1, pred, visited, graph); } } //if(has_blue_cycle_with_one_red) // cout << "true" << endl; //else // cout << "false" << endl; if (has_red_cycle_with_one_blue || has_blue_cycle_with_one_red) cout << "NO" << endl; else cout << "YES" << endl; } int main() { cin.tie(0); cout.tie(0); cin.exceptions(ios::failbit); ios::sync_with_stdio(false); ll t = 1; // cin >> t; // potential comment while (t--) { test_case(); } return 0; }