#include using namespace std; #ifdef LOCAL #include "debug.h" #else #define DEBUG(...) #endif struct scc { const int n; vector> edge_pool; vector head, g, ord, low, st, cmp; int t = 0, id = 0; scc(int _n) : n(_n), head(n + 1), ord(n, -1), low(n), cmp(n, -1) {} void add(int from, int to) { edge_pool.emplace_back(from, to); ++head[from]; } void build() { for (int v = 0; v < n; ++v) head[v + 1] += head[v]; g.resize(head[n]); for (auto e : edge_pool) g[--head[e.first]] = e.second; } void dfs(int v) { ord[v] = low[v] = t++, st.push_back(v); for (int i = head[v]; i < head[v + 1]; ++i) { int to = g[i]; if (ord[to] == -1) { dfs(to); low[v] = min(low[v], low[to]); } else if (cmp[to] == -1) { low[v] = min(low[v], ord[to]); } } if (ord[v] == low[v]) { while (true) { int u = st.back(); st.pop_back(); cmp[u] = id; if (u == v) break; } ++id; } } void run() { build(); for (int v = 0; v < n; ++v) if (ord[v] == -1) dfs(v); for (auto&& e : cmp) e = id - e - 1; } }; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, m; cin >> n >> m; scc g(n); vector a(m), b(m), c(m); for (int i = 0; i < m; ++i) { cin >> a[i] >> b[i] >> c[i]; --a[i], --b[i]; if (c[i] == 1) { g.add(a[i], b[i]); g.add(b[i], a[i]); } else { g.add(a[i], b[i]); } } g.run(); vector cv(g.id), ce(g.id); for (int v = 0; v < n; ++v) { ++cv[g.cmp[v]]; DEBUG(v, g.cmp[v]); } for (int i = 0; i < m; ++i) { if (g.cmp[a[i]] == g.cmp[b[i]]) { ++ce[g.cmp[a[i]]]; } } for (int i = 0; i < g.id; ++i) { if (cv[i] == 1) { continue; } if (cv[i] == 2 and ce[i] == 1) { continue; } cout << "Yes\n"; exit(0); } cout << "No\n"; }