#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); vector sp(n + 1); sp[0] = 2; for (int i = 0; i < m; ++i) { int u, v, c; cin >> u >> v >> c; E[i] = {u, v}; G[u].emplace_back(-c, i); if (c == 1) G[v].emplace_back(-c, i); else sp[u] = -1; } for (int i = 1; i <= n; ++i) sort(G[i].begin(), G[i].end()); vector p(n + 1); for (int i = 1; i <= n; ++i) p[i] = i; sort(p.begin(), p.end(), [&] (const int& x, const int& y) { return sp[x] < sp[y]; }); int ans = 0; vector vis(n + 1), stk(n + 1), evis(m); function dfs = [&] (int u) { vis[u] = stk[u] = 1; for (auto& [c, 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 (auto o : p) if (!vis[o] && o) dfs(o); cout << (ans ? "Yes" : "No") << '\n'; }