#include #include #include #include #include #include #include using namespace std; using ll = long long; struct Graph { struct Vertex { int n, f, g; }; struct Edge { int i, n, c; }; Graph(int n, int m) : v(n, { -1, -1, 0 }), e(m), n(n), m(0) {} void add_edge(int i, int j, int c) { e[m] = { j, v[i].n, c }; v[i].n = m; m++; } int dfs(int i, int p, int c, int s) { if (v[i].f == s && v[i].g) return 1; if (v[i].f >= 0) return 0; v[i].f = s; v[i].g = 1; for (int j = v[i].n; j >= 0; j = e[j].n) { Edge& o = e[j]; if (o.i == p) { if (c == 1 && o.c == 1) continue; } if (dfs(o.i, i, o.c, s)) return 1; } v[i].g = 0; return 0; } void solve() { int b = 0; for (int i = 0; i < n; i++) { if (v[i].f >= 0) continue; if (dfs(i, -1, 0, i)) { b = 1; break; } } cout << (b ? "Yes" : "No") << endl; } vector v; vector e; int n, m; }; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; Graph g(n, m * 2); for (int i = 0; i < m; i++) { int a, b, c; cin >> a >> b >> c; a--; b--; g.add_edge(a, b, c); if (c == 1) g.add_edge(b, a, c); } g.solve(); return 0; }