#include #include using namespace std; #define RED 1 #define BLUE 0 struct Node{ Node() { connections.reserve(100); colors.reserve(100); } vector connections; vector colors; }; vector nodes; vector nodesOneColor; vector visited; bool loop(int start, int cur, int bc, int rc, int depth) { //cout << "NODE: " << cur << '\n'; if (visited[cur]) { if (cur == start) { if (rc != 0 && bc != 0) { if (rc == 1 || bc == 1) { return false; } } } return true; } if (depth <= 0) { return true; } if (rc > 1 && bc > 1) { return true; } Node& n = nodes[cur]; int firstI = 0; for (int i = 0; i < n.connections.size(); i++) { if (n.connections[i] == start) { firstI = i; break; } } { int i = firstI; int edge = n.connections[i]; bool color = n.colors[i]; //cout << "EDGE: " << cur << ' ' << edge << " COLOR: " << color << '\n'; if (color == 1) { ++rc; } else { ++bc; } visited[cur] = true; if (!loop(start, edge, bc, rc, depth - 1)) { visited[cur] = false; return false; } visited[cur] = false; if (color == 1) { --rc; } else { --bc; } } for (int i = 0; i < n.connections.size(); i++) { if (firstI == i) { continue; } int edge = n.connections[i]; bool color = n.colors[i]; //cout << "EDGE: " << cur << ' ' << edge << " COLOR: " << color << '\n'; if (color == 1) { ++rc; } else { ++bc; } visited[cur] = true; if (!loop(start, edge, bc, rc, depth - 1)) { visited[cur] = false; return false; } visited[cur] = false; if (color == 1) { --rc; } else { --bc; } } return true; } int cN, cE; bool isOk() { for (int d = max(cN / 2, 3); d <= cN; d = max(cN, d + 3)) { for (int i = 0; i < nodesOneColor.size(); ++i) { //cout << "ROUND: " << i << '\n'; if (!loop(nodesOneColor[i], nodesOneColor[i], 0, 0, d)) { return false; } } if (d == cN) { d++; } } return true; } int main() { cin >> cN >> cE; nodes.reserve(cN); for (int i = 0; i < cN; ++i){ nodes.emplace_back(); } visited.resize(cN); visited.clear(); for (int i = 0; i < cN; i++) { int x1, x2, col; cin >> x1 >> x2 >> col; { Node &n = nodes[x1]; n.colors.push_back(col); n.connections.push_back(x2); } { Node &n = nodes[x2]; n.colors.push_back(col); n.connections.push_back(x1); } if (col == 1) { nodesOneColor.push_back(x1); nodesOneColor.push_back(x2); } } if (isOk()) { cout << "YES\n"; } else { cout << "NO\n"; } return 0; }