#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define llint long long #define inf 1e18 #define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++) #define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++) #define chmin(x, y) (x) = min((x), (y)) #define chmax(x, y) (x) = max((x), (y)) using namespace std; typedef pair P; llint n, m; vector G[200005], revG[200005]; vector G2[200005]; vector topo; bool used[200005]; int scc[200005]; void tpsort(int v) { used[v] = true; for(int i = 0; i < G[v].size(); i++){ if(!used[G[v][i]]) tpsort(G[v][i]); } topo.push_back(v); } void sccdfs(int v, int id) { used[v] = true; scc[v] = id; for(int i = 0; i < revG[v].size(); i++){ if(!used[revG[v][i]]) sccdfs(revG[v][i], id); } } bool dfs(int v, int p) { used[v] = true; bool ret = false; for(int i = 0; i < G2[v].size(); i++){ llint u = G2[v][i]; if(u == p) continue; if(scc[v] != scc[u]) continue; if(used[u]){ ret = true; continue; } ret |= dfs(u, v); } return ret; } int main(void) { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m; llint u, v, t; for(int i = 1; i <= m; i++){ cin >> u >> v >> t; G[u].push_back(v), revG[v].push_back(u); if(t == 1) G[v].push_back(u), revG[u].push_back(v); G2[u].push_back(v), G2[v].push_back(u); } for(int i = 1; i <= n; i++) if(!used[i]) tpsort(i); reverse(topo.begin(), topo.end()); int id = 0; for(int i = 1; i <= n; i++) used[i] = false; for(int i = 0; i < topo.size(); i++) if(!used[topo[i]]) sccdfs(topo[i], id++); for(int i = 1; i <= n; i++) used[i] = false; for(int i = 1; i <= n; i++){ if(used[i]) continue; if(dfs(i, -1)){ cout << "Yes" << endl; return 0; } } cout << "No" << endl; return 0; }