#include using namespace std; typedef long long ll; typedef pair l_l; typedef pair i_i; template inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } template inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } const long double EPS = 1e-10; const long long INF = 1e18; const long double PI = acos(-1.0L); //const ll mod = 1000000007; struct UnionFind { vector par; vector rank; vector Size; UnionFind(int n = 1) { init(n); } void init(int n = 1) { par.resize(n + 1); rank.resize(n + 1); Size.resize(n + 1); for (int i = 0; i <= n; ++i) par[i] = i, rank[i] = 0, Size[i] = 1; } int root(int x) { if (par[x] == x) { return x; } else { int r = root(par[x]); return par[x] = r; } } bool issame(int x, int y) { return root(x) == root(y); } bool merge(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y); if (rank[x] == rank[y]) ++rank[x]; par[y] = x; Size[x] += Size[y]; return true; } ll size(int x){ return Size[root(x)]; } }; typedef vector> UnWeightedGraph; template< typename G > struct StronglyConnectedComponents { const G &g; UnWeightedGraph gg, rg; vector< int > comp, order, used; StronglyConnectedComponents(G &g) : g(g), gg(g.size()), rg(g.size()), comp(g.size(), -1), used(g.size()) { for(int i = 0; i < g.size(); i++) { for(auto e : g[i]) { gg[i].emplace_back((int) e); rg[(int) e].emplace_back(i); } } } int operator[](int k) { return (comp[k]); } void dfs(int idx) { if(used[idx]) return; used[idx] = true; for(int to : gg[idx]) dfs(to); order.push_back(idx); } void rdfs(int idx, int cnt) { if(comp[idx] != -1) return; comp[idx] = cnt; for(int to : rg[idx]) rdfs(to, cnt); } void build(UnWeightedGraph &t) { for(int i = 0; i < gg.size(); i++) dfs(i); reverse(begin(order), end(order)); int ptr = 0; for(int i : order) if(comp[i] == -1) rdfs(i, ptr), ptr++; t.resize(ptr); for(int i = 0; i < g.size(); i++) { for(auto &to : g[i]) { int x = comp[i], y = comp[to]; if(x == y) continue; t[x].push_back(y); } } } }; vector e; int main() { ll N, M; cin >> N >> M; UnionFind uni(N); for(int i = 0; i < M; i++) { ll a, b, c; cin >> a >> b >> c; a--; b--; if(c == 1) { if(!uni.merge(a, b)) { cout << "Yes" << endl; return 0; } } else { e.push_back({a, b}); } } ll V = 0; map inv; for(int i = 0; i < N; i++) { if(uni.root(i) == i) { inv[i] = V; V++; } } for(int i = 0; i < N; i++) { inv[i] = inv[uni.root(i)]; } UnWeightedGraph g(V), t; StronglyConnectedComponents SCC(g); set st; for(auto tmp : e) { if(inv[tmp.first] == inv[tmp.second]) { cout << "Yes" << endl; return 0; } //cerr << inv[tmp.first] << " " << inv[tmp.second] << endl; if(st.count({inv[tmp.second], inv[tmp.first]})) { cout << "Yes" << endl; return 0; } st.insert({inv[tmp.first], inv[tmp.second]}); g[inv[tmp.first]].push_back(inv[tmp.second]); } SCC.build(t); set st2; for(int i = 0; i < V; i++) { //cerr << i << " " << SCC[i] << endl; if(st2.count(SCC[i])) { cout << "Yes" << endl; return 0; } st2.insert(SCC[i]); } cout << "No" << endl; return 0; }