#include using namespace std; using i64 = long long; #define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++) #define all(x) x.begin(),x.end() template static inline std::vector ndvec(size_t&& n, T val) noexcept { return std::vector(n, std::forward(val)); } template static inline auto ndvec(size_t&& n, Tail&&... tail) noexcept { return std::vector(tail)...))>(n, ndvec(std::forward(tail)...)); } template struct chain { Cond cond; chain(Cond cond) : cond(cond) {} bool operator()(T& a, const T& b) const { if(cond(a, b)) { a = b; return true; } return false; } }; template chain make_chain(Cond cond) { return chain(cond); } #include using namespace std; using i64 = long long; vector strongly_connected_components(const vector>& g) { i64 n = g.size(); vector> rg(n); for(int i = 0;i < n;i++) { for(auto j: g[i]) { rg[j].push_back(i); } } vector vis(n,false); vector vs; vector res(n,-1); function dfs = [&](int v) { vis[v] = true; for(auto& t : g[v]) { if(!vis[t]) dfs(t); } vs.push_back(v); }; function rdfs = [&](int v,int k) { vis[v] = true; res[v] = k; for(auto to : rg[v]) { if(!vis[to]) rdfs(to,k); } }; for(int i = 0;i < n;i++) { if(!vis[i]) dfs(i); } vis.assign(n,false); int k = 0; for(int i = n - 1;i >= 0;i--) { if(!vis[vs[i]]) rdfs(vs[i] , k++); } return res; } vector> G; int main() { i64 N, M; cin >> N >> M; G.resize(N * 3); rep(i,0,N) { G[i * 3].push_back(i * 3 + 1); G[i * 3 + 1].push_back(i * 3 + 2); } rep(i,0,M) { i64 a, b, c; cin >> a >> b >> c; a--; b--; if(c == 1) { G[a * 3 + 1].push_back(b * 3 + 2); G[b * 3 + 1].push_back(a * 3 + 2); } else { G[a * 3 + 2].push_back(b * 3); } } auto res = strongly_connected_components(G); i64 ans = *std::max_element(all(res)); if(ans + 1 == N * 3) { cout << "No" << endl; } else { cout << "Yes" << endl; } }