#include using namespace std; int main(){ int N, M; cin >> N >> M; vector> E1(N), E2(N); for (int i = 0; i < M; i++){ int a, b, c; cin >> a >> b >> c; a--; b--; if (c == 1){ E1[a].push_back(b); E1[b].push_back(a); } if (c == 2){ E2[a].push_back(b); } } int gcount = 0; vector G(N, -1); for (int i = 0; i < N; i++){ if (G[i] == -1){ G[i] = gcount; queue Q; Q.push(i); while (!Q.empty()){ int v = Q.front(); Q.pop(); for (int w : E1[v]){ if (G[w] == -1){ G[w] = gcount; Q.push(w); } } } gcount++; } } vector Vcount(gcount, 0); for (int i = 0; i < N; i++){ Vcount[G[i]]++; } vector Ecount(gcount, 0); for (int i = 0; i < N; i++){ for (int j : E1[i]){ Ecount[G[i]]++; } } vector> E3(gcount); bool self_loop = false; for (int i = 0; i < N; i++){ for (int j : E2[i]){ E3[G[i]].insert(G[j]); } } //トポロジカルソートを試みる vector indeg(gcount, 0); for (int i = 0; i < gcount; i++){ for (int j : E3[i]){ indeg[j]++; } } queue Q; for (int i = 0; i < gcount; i++){ if (indeg[i] == 0){ Q.push(i); } } int count = 0; while (!Q.empty()){ int v = Q.front(); Q.pop(); count++; for (int w : E3[v]){ indeg[w]--; if (indeg[w] == 0){ Q.push(w); } } } if (count != gcount){ cout << "Yes" << endl; } else { bool res = false; for (int i = 0; i < gcount; i++){ if (Ecount[i] >= Vcount[i]){ res = true; } } if (res){ cout << "Yes" << endl; } else { cout << "No" << endl; } } }