#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } constexpr long long MAX = 5100000; constexpr long long INF = 1LL << 60; constexpr int inf = 1 << 28; constexpr long long mod = 1000000007LL; //constexpr long long mod = 998244353LL; using namespace std; typedef unsigned long long ull; typedef long long ll; class UnionFind { public: vector Parent; UnionFind(int N) { Parent = vector(N, -1); } int root(int A) { if (Parent[A] < 0) { return A; } return Parent[A] = root(Parent[A]); } long long size(int A) { return -(long long)Parent[root(A)]; } bool connect(int A, int B) { A = root(A); B = root(B); if (A == B) { return false; } if (size(A) < size(B)) { swap(A, B); } Parent[A] += Parent[B]; Parent[B] = A; return true; } }; int main() { /* cin.tie(nullptr); ios::sync_with_stdio(false); */ int n, m; scanf("%d %d", &n, &m); vector a(m), b(m), c(m); for (int i = 0; i < m; i++) scanf("%d %d %d", &a[i], &b[i], &c[i]), a[i]--, b[i]--, c[i]--; UnionFind uni(n); for (int i = 0; i < m; i++) { if (c[i]) continue; if (uni.root(a[i]) == uni.root(b[i])) { puts("Yes"); return 0; } uni.connect(a[i], b[i]); } vector> g(n); vector deg(n); for (int i = 0; i < m; i++) { if (c[i]) { g[uni.root(a[i])].push_back(uni.root(b[i])); deg[uni.root(b[i])]++; } } queue q; vector topo; for (int i = 0; i < n; i++) { if (deg[i] == 0) { q.push(i); topo.push_back(i); } } while (!q.empty()) { int cur = q.front(); q.pop(); for (int next : g[cur]) { deg[next]--; if (deg[next] == 0) { q.push(next); topo.push_back(next); } } } if (topo.size() == n) puts("No"); else puts("Yes"); return 0; }