#pragma gcc optimize("Ofast") #include using namespace std; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, m; cin >> n >> m; vector> G(n + 1); vector> E(m); for (int i = 0; i < m; ++i) { int u, v, c; cin >> u >> v >> c; E[i] = {u, v}; G[u].emplace_back(i); if (c == 1) G[v].emplace_back(i); } int ans = 0; vector vis(n + 1), stk(n + 1), evis(m); function dfs = [&] (int u) { vis[u] = stk[u] = 1; for (int i : G[u]) { if (evis[i]) continue; int v = u != E[i].first ? E[i].first : E[i].second; if (vis[v] && stk[v]) ans = 1; if (!vis[v]) { evis[i] = 1; dfs(v); } } stk[u] = 0; }; for (int i = 1; i <= n; ++i) if (!vis[i]) dfs(i); cout << (ans ? "Yes" : "No") << '\n'; }