#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 = 1000000007; //constexpr long long mod = 1000000007LL; //constexpr long long mod = 998244353LL; using namespace std; typedef unsigned long long ull; typedef long long ll; using UnWeightedGraph = vector>; 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); } } } }; 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> g(n); vector> v; UnionFind uni(n); int cnt = 0; int edges = 0; for (int i = 0; i < m; i++) { int a, b, c; scanf("%d %d %d", &a, &b, &c); a--; b--; if (c == 1) { if (!uni.connect(a, b)) { puts("Yes"); return 0; } } else v.emplace_back(a, b); } for (auto p : v) { if (uni.root(p.first) == uni.root(p.second)) { puts("Yes"); return 0; } g[uni.root(p.first)].push_back(uni.root(p.second)); } StronglyConnectedComponents>> scc(g); vector> t; scc.build(t); if (t.size() < n) puts("Yes"); else puts("No"); return 0; }