#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef vector vl; typedef vector vvl; typedef vector vc; typedef vector vs; typedef vector vb; typedef vector vd; typedef pair P; typedef vector

vpl; typedef tuple tapu; #define rep(i,n) for(ll i=0; i<(n); i++) #define REP(i,a,b) for(int i=(a); i<(b); i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() const int inf = 1<<30; const ll linf = 1LL<<62; const int MAX = 510000; ll dy[8] = {0,1,0,-1,1,-1,1,-1}; ll dx[8] = {1,0,-1,0,1,-1,-1,1}; const double pi = acos(-1); const double eps = 1e-7; template inline bool chmin(T1 &a,T2 b){ if(a>b){ a = b; return true; } else return false; } template inline bool chmax(T1 &a,T2 b){ if(a inline void print2(T1 a, T2 b){cout << a << " " << b << endl;} template inline void print3(T1 a, T2 b, T3 c){ cout << a << " " << b << " " << c << endl; } const int mod = 1e9 + 7; //const int mod = 998244353; struct UnionFind{ vl p; vl rank; vl cnt; UnionFind(ll n){ rank.resize(n,0); p.resize(n,0); cnt.resize(n,0); rep(i,n){ p[i] = i; rank[i] = 0; cnt[i] = 1; } } ll find(ll x){ if(x != p[x]) p[x] = find(p[x]); return p[x]; } bool same(ll x, ll y){ return find(x) == find(y); } void unite(ll x, ll y){ x = find(x); y = find(y); if(x == y) return; if(rank[x] > rank[y]){ p[y] = x; cnt[x] += cnt[y]; }else{ p[x] = y; cnt[y] += cnt[x]; if(rank[x] == rank[y]) rank[y]++; } } ll size(ll x){ return cnt[find(x)]; } }; int main(){ ll n,m; cin >> n >> m; vector edges(2); UnionFind uf(n); rep(i,m){ ll a,b,c; cin >> a >> b >> c; a--; b--; c--; edges[c].emplace_back(a,b); if(!c){ if(uf.same(a,b)){ puts("Yes"); return 0; } uf.unite(a,b); } } vl visit(n,false); vvl g(n); vl indeg(n,0); for(auto i : edges[1]){ ll a = uf.find(i.first); ll b = uf.find(i.second); g[a].push_back(b); indeg[b]++; } rep(i,n) visit[i] = false; rep(i,n){ if(g[i].empty() || visit[i] || indeg[i]) continue; visit[i] = true; queue q; q.push(i); while(!q.empty()){ ll u = q.front(); q.pop(); for(auto v : g[u]){ indeg[v]--; if(!indeg[v] && !visit[v]){ visit[v] = true; q.push(v); } } } } rep(i,n){ if(indeg[i]){ puts("Yes"); return 0; } } puts("No"); }