#define _USE_MATH_DEFINES #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; const int INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 1000000007; // const int MOD = 998244353; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(20); } } iosetup; using CostType = char; struct Edge { int src, dst; CostType cost; Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {} inline bool operator<(const Edge &x) const { return cost != x.cost ? cost < x.cost : dst != x.dst ? dst < x.dst : src < x.src; } inline bool operator<=(const Edge &x) const { return !(x < *this); } inline bool operator>(const Edge &x) const { return x < *this; } inline bool operator>=(const Edge &x) const { return !(*this < x); } }; vector topological_sort(const vector> &graph) { int n = graph.size(); vector deg(n, 0); REP(i, n) { for (const Edge &e : graph[i]) ++deg[e.dst]; } queue que; REP(i, n) { if (deg[i] == 0) que.emplace(i); } vector res; while (!que.empty()) { int ver = que.front(); que.pop(); res.emplace_back(ver); for (const Edge &e : graph[ver]) { if (--deg[e.dst] == 0) que.emplace(e.dst); } } return res.size() == n ? res : vector(); } int main() { int n, m; cin >> n >> m; set> und, dir; while (m--) { int a, b, c; cin >> a >> b >> c; --a; --b; (c == 1 ? und : dir).emplace(a, b); } vector p, q; for (auto [a, b] : dir) { if (und.count(make_pair(a, b)) == 1 || und.count(make_pair(b, a)) == 1 || dir.count(make_pair(b, a)) == 1) { cout << "Yes\n"; return 0; } p.emplace_back(a); q.emplace_back(b); } vector> und_graph(n); for (auto [a, b] : und) { und_graph[a].emplace_back(b); und_graph[b].emplace_back(a); } vector id(n, -1); int sz = 0; REP(i, n) { if (id[i] != -1) continue; function dfs = [&](int par, int ver) { for (int e : und_graph[ver]) { if (e == par) continue; if (id[e] == -1) { id[e] = id[i]; dfs(ver, e); } else { cout << "Yes\n"; exit(0); } } }; id[i] = sz++; dfs(-1, i); } // REP(i, n) cout << id[i] << " \n"[i + 1 == n]; vector> graph(sz); REP(i, p.size()) { int a = id[p[i]], b = id[q[i]]; if (a == b) { cout << "Yes\n"; return 0; } // cout << a << ' ' << b << '\n'; graph[a].emplace_back(a, b); } cout << (topological_sort(graph).empty() ? "Yes\n" : "No\n"); return 0; }