#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define int long long #define all(a) a.begin(),a.end() #define rep(i, n) for (ll i = 0; i < (n); i++) #define pb push_back using namespace std; typedef long long ll; typedef pair P; typedef long double ld; typedef complex com; constexpr ll INF = 1000000000000000000; constexpr ld EPS = 1e-12; constexpr ld PI = 3.141592653589793238; template inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; } class unionfind { vector par; vector sz; public: unionfind(int n) { par = vector(n); for (int i = 0; i < n; i++) par[i] = i; sz = vector(n, 1); } int find(int x) { if (par[x] == x) return x; else return par[x] = find(par[x]); } int si(int x) { return sz[find(x)]; } bool same(int x, int y) { return find(x) == find(y); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (sz[x] < sz[y]) { par[x] = y; sz[y] += sz[x]; } else { par[y] = x; sz[x] += sz[y]; } } }; vector> graph(100010, vector()); vector vis(100010); bool ans = false; signed main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); int n, m; cin >> n >> m; unionfind uni(n); vector

edge; rep(i, m) { int u, v; cin >> u >> v; u--; v--; int c; cin >> c; if (c == 1) uni.unite(u, v); else edge.pb(P(u, v)); } vector deg(n); for (P p : edge) { if (uni.find(p.first) == uni.find(p.second)) { cout << "Yes" << endl; return 0; } graph[uni.find(p.first)].pb(uni.find(p.second)); deg[uni.find(p.second)]++; } queue que; rep(i, n) { int r = uni.find(i); if (!vis[r] && deg[r] == 0) { vis[r] = true; que.push(r); } } while (!que.empty()) { int v = que.front(); que.pop(); vis[v] = true; for (int i : graph[v]) { vis[i] = true; /*if (deg[i] == 0) { ans = true; break; }*/ //else { deg[i]--; if (deg[i] == 0) que.push(i); //} } } rep(i, n) { if (!vis[uni.find(i)]) ans = true; } cout << (ans ? "Yes" : "No") << endl; }