//#define _GLIBCXX_DEBUG #include #define rep(i, n) for(int i=0; i; using vs = vector; using vi = vector; using vvi = vector; template using PQ = priority_queue; template using PQG = priority_queue, greater >; const int INF = 0xccccccc; const ll LINF = 922337203685477580LL; template inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);} template inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);} template istream &operator>>(istream &is, pair &p) { return is >> p.first >> p.second;} template ostream &operator<<(ostream &os, const pair &p) { return os << p.first << ' ' << p.second;} struct UnionFind { vector par; // 親を指すvector,-par[親]は木のサイズ UnionFind(int n):par(n, -1) {} // uniteで親を埋め込んでいく必要あり int root(int x) { // 親をたどる&データの整理 if(par[x] < 0) return x; return par[x] = root(par[x]); } bool unite(int x, int y) { // データの結合 x = root(x); y = root(y); if(x == y) return false; if(par[x] > par[y]) swap(x, y); par[x] += par[y]; par[y] = x; return true; } bool same(int x, int y) {return root(x) == root(y);} // 所属判定 int size(int x) {return -par[root(x)];} // 木のサイズ }; const int N = 1e5+10; //head int n, m; UnionFind uft(N); vector

edge; bool ok; int num[N]; vi G[N]; bitset check; bitset F; bool dfs(int i) { for(int ne:G[i]) { if(F.test(ne)) { return true; } if(!check.test(ne)) { check.set(ne); F.set(ne); if(dfs(ne)) return true; F.flip(ne); } } return false; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; rep(i, m) { int a, b, c; cin >> a >> b >> c; a--; b--; if(c == 1) { if(!uft.unite(a, b)) ok = true; } else { edge.emplace_back(a, b); } } int cnt = 0; rep(i, n) if(uft.root(i) == i) { num[i] = cnt++; } rep(i, edge.size()) { int u, v; tie(u, v) = edge[i]; u = uft.root(u); v = uft.root(v); u = num[u]; v = num[v]; if(u == v) ok = true; else { G[u].emplace_back(v); } } rep(i, cnt) if(!check.test(i)) { check.set(i); F.set(i); if(dfs(i)) ok = true; F.flip(i); } cout << (ok?"Yes":"No") << endl; }