#include using i32 = int_fast32_t; using i64 = int_fast64_t; using u32 = uint_fast32_t; using u64 = uint_fast64_t; using f64 = double; using f80 = long double; #define FOR(v, a, b) for(i64 v = (a); v < (b); ++v) #define FORE(v, a, b) for(i64 v = (a); v <= (b); ++v) #define REP(v, n) FOR(v, 0, n) #define REPE(v, n) FORE(v, 0, n) #define REV(v, a, b) for(i64 v = (a); v >= (b); --v) #define ALL(x) (x).begin(), (x).end() #define RALL(x) (x).rbegin(), (x).rend() #define ITR(it, c) for(auto it = (c).begin(); it != (c).end(); ++it) #define RITR(it, c) for(auto it = (c).rbegin(); it != (c).rend(); ++it) #define EXIST(c,x) ((c).find(x) != (c).end()) #define fst first #define snd second #define UNIQ(v) (v).erase(unique(ALL(v)), (v).end()) #define bit(i) (1LL<<(i)) #ifdef DEBUG #include #else #define dump(...) ((void)0) #endif using namespace std; template void join(ostream &ost, I s, I t, string d=" "){for(auto i=s; i!=t; ++i){if(i!=s)ost< istream& operator>>(istream &is, vector &v){for(auto &a : v) is >> a; return is;} template void pout(const T &value){std::cout << value << "\n";} template void pout(const T &value, const Args&... args){std::cout << value << " ";pout(args...);} template bool chmin(T &a, const U &b){return (a>b ? a=b, true : false);} template bool chmax(T &a, const U &b){return (a void fill_array(T (&a)[N], const U &v){fill((U*)a, (U*)(a+N), v);} template auto make_vector(int n, int m, const T &value){return vector>(n, vector(m, value));} struct Init{ Init(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(12); cerr << fixed << setprecision(12); } }init; template class Edge{ public: int from,to; Cost cost; Edge() {} Edge(int to, Cost cost): to(to), cost(cost){} Edge(int from, int to, Cost cost): from(from), to(to), cost(cost){} Edge rev() const {return Edge(to,from,cost);} friend std::ostream& operator<<(std::ostream &os, const Edge &e){ os << "(FROM: " << e.from << "," << "TO: " << e.to << "," << "COST: " << e.cost << ")"; return os; } }; template using Graph = std::vector>>; template using Tree = std::vector>>; template void add_edge(C &g, int from, int to, T w){ g[from].emplace_back(from, to, w); } template void add_undirected(C &g, int a, int b, T w){ add_edge(g, a, b, w); add_edge(g, b, a, w); } class UnionFind{ std::vector parent, depth, size; int count; public: UnionFind(int n): parent(n), depth(n,1), size(n,1), count(n){ std::iota(parent.begin(), parent.end(), 0); } inline int get_root(int i){ if(parent[i] == i) return i; else return parent[i] = get_root(parent[i]); } inline bool is_same(int i, int j){return get_root(i) == get_root(j);} inline int merge(int i, int j){ int ri = get_root(i), rj = get_root(j); if(ri == rj) return ri; else{ --count; if(depth[ri] < depth[rj]){ parent[ri] = rj; size[rj] += size[ri]; return rj; }else{ parent[rj] = ri; size[ri] += size[rj]; if(depth[ri] == depth[rj]) ++depth[ri]; return ri; } } } inline int get_size(int i){return size[get_root(i)];} inline int count_group(){return count;} }; template struct SCC{ std::vector result; int scc_size; private: std::vector visit; std::vector check; void dfs(int cur, const Graph &graph){ visit[cur] = true; for(auto &e : graph[cur]){ if(not visit[e.to]) dfs(e.to,graph); } check.push_back(cur); } void rdfs(int cur, int i, const Graph &rgraph){ result[cur] = i; for(auto &e : rgraph[cur]){ if(result[e.to] == -1) rdfs(e.to,i,rgraph); } } public: SCC(const Graph &graph){ const int n = graph.size(); result.assign(n, -1); visit.assign(n, false); for(int i = 0; i < n; ++i) if(!visit[i]) dfs(i,graph); std::reverse(check.begin(), check.end()); Graph rgraph(n); for(int i = 0; i < n; ++i) for(auto &e : graph[i]) rgraph[e.to].push_back(e.rev()); int i = 0; for(auto c : check) if(result[c] == -1) {rdfs(c,i,rgraph); ++i;} scc_size = i; } inline const int operator[](int i) const {return result[i];} }; template bool detect_cycle(const Graph &g){ const int N = g.size(); std::vector check(N); auto dfs = [&](auto &dfs, int cur) -> bool{ if(check[cur] == 2) return true; check[cur] = 2; for(auto &e : g[cur]){ if(check[cur] != 1){ if(dfs(dfs, e.to)) return true; } } check[cur] = 1; return false; }; for(int i = 0; i < N; ++i){ if(check[i] == 0 and dfs(dfs, i)) return true; } return false; } int main(){ int N, M; while(cin >> N >> M){ Graph g(N); UnionFind uf(N); bool ans = false; vector> de; REP(i,M){ int a, b, c; cin >> a >> b >> c; --a, --b; if(c == 1){ if(uf.is_same(a, b)) ans = true; uf.merge(a, b); }else{ de.emplace_back(a, b); } } for(auto &[a,b] : de){ add_edge(g, uf.get_root(a), uf.get_root(b), 1); } if(detect_cycle(g)) ans = true; pout(ans ? "Yes" : "No"); } return 0; }