#include #include #include #include #include #include #include #include #include #include #include #include #include #define FOR(i, n, m) for(ll i = n; i < (int)m; i++) #define REP(i, n) FOR(i, 0, n) #define ALL(v) v.begin(), v.end() #define pb push_back using namespace std; using ll = long long; using P = pair; constexpr ll inf = 1000000000; constexpr ll mod = 998244353; constexpr long double eps = 1e-9; struct Edge { int to; int ty; }; vector> g; vector> ng; void dfs(int v, vector& gr, int c) { gr[v] = c; for(auto elm: g[v]) { if(gr[elm.to] != -1 || elm.ty == 2) continue; dfs(elm.to, gr, c); } return; } bool dfs2(int v, vector& used) { used[v] = true; bool ret = false; for(auto elm: ng[v]) { if(used[elm]) ret = true; else ret = ret | dfs2(elm, used); } return ret; } int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m; cin >> n >> m; g.resize(n); REP(i, m) { int a, b, c; cin >> a >> b >> c; a--; b--; if(c == 1) { g[a].pb({b, c}); g[b].pb({a, c}); } else { g[a].pb({b, c}); } } vector gr(n, -1), grc, grec; int c = 0; REP(i, n) { if(gr[i] != -1) continue; dfs(i, gr, c); c++; } grc.assign(c, 0); grec.assign(c, 0); ng.resize(c); REP(i, n) { grc[gr[i]]++; for(auto elm: g[i]) { if(gr[i] != gr[elm.to]) { ng[gr[i]].pb(gr[elm.to]); } else { if(elm.ty == 2 || i < elm.to) grec[gr[i]]++; } } } REP(i, c) { if(grc[i] <= grec[i]) { cout << "Yes" << endl; return 0; } } REP(i, c) { sort(ng[i].begin(), ng[i].end()); ng[i].erase(unique(ng[i].begin(), ng[i].end()), ng[i].end()); } vector used(c, false); vector indeg(c, 0); REP(i, c) { for(auto adj: ng[i]) indeg[adj]++; } ng.pb({}); REP(i, c) { if(indeg[i] == 0) ng[c].pb(i); } bool ok = false; if((int)ng[c].size() == 0 || dfs2(c, used)) cout << "Yes" << endl; else cout << "No" << endl; return 0; }