#define _USE_MATH_DEFINES #include using namespace std; ///////////////////////////////////////////////////////////////////////// // UNION FIND ///////////////////////////////////////////////////////////////////////// class UnionFind { private: int siz; vector a; public: UnionFind(int x) : siz(x), a(x, -1) {} int root(int x) { return a[x] < 0 ? x : a[x] = root(a[x]); } bool unite(int x, int y) { x = root(x); y = root(y); if (x == y) return false; siz--; if (a[x] > a[y]) swap(x, y); a[x] += a[y]; a[y] =x; return true; } bool same(int x, int y) { return root(x) == root(y); } int size(int x) { return -a[root(x)]; } int connected_component() { return siz; } }; ///////////////////////////////////////////////////////////////////////// // END UNION FIND ///////////////////////////////////////////////////////////////////////// vector> g; vector vis; void dfs (int cur) { if (vis[cur] == 2) return; if (vis[cur] == 1) { cout << "Yes" << endl; exit(0); } vis[cur] = 1; for (int nxt : g[cur]) { dfs(nxt); } vis[cur] = 2; } signed main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector> de; UnionFind uf(n); for (int i = 0; i < m; i++) { int u, v, t; cin >> u >> v >> t; u--; v--; if (t == 1) { if (!uf.unite(u, v)) { cout << "Yes" << endl; return 0; } } else { de.emplace_back(u, v); } } g = vector>(n); vis = vector(n); for (auto& p : de) { p.first = uf.root(p.first); p.second = uf.root(p.second); g[p.first].insert(p.second); } for (int i = 0; i < n; i++) { if (!vis[i]) dfs(i); } cout << "No" << endl; return 0; }