#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; class range {private: struct I{int x;int operator*(){return x;}bool operator!=(I& lhs){return x ostream& operator<<(ostream& os, const pair& p){ return os << "{" << p.first << ", " << p.second << "}"; } template ostream& operator<<(ostream& os, const vector& obj) { os << "{"; for (const auto& e : obj) os << e << ", "; return os << "}"; } template ostream& operator<<(ostream& os, const set& obj) { os << "set{"; for (const auto& e : obj) os << e << ", "; return os << "}"; } template ostream& operator<<(ostream& os, const map& obj) { os << "map{"; for (const auto& e : obj) os << e << ", "; return os << "}"; } #ifdef ONLINE_JUDGE #define dump(expr) ; #else #define dump(expr) { cerr << "\033[33m#L" << __LINE__ << ": " << expr << "\033[39m" << endl; } #endif struct E { int fr, to, c; E(int fr_, int to_, int c_) : fr(fr_), to(to_), c(c_) {} friend ostream &operator<<(ostream &os, const E &e) { os << "(" << e.fr << " -> " << e.to << ")"; return os; } }; typedef vector V; // typedef vector G; // note that each vertex will have both enterning edges & entered edges void add_edge(vector &vg, int fr, int to, int c) { vg[fr].push_back(E(fr, to, c)); } ll solve() { int n, m; cin >> n >> m; vector vg(n + 1); for (int i : range(m)) { int a, b, c; cin >> a >> b >> c; add_edge(vg, a, b, c); if (c == 1) { add_edge(vg, b, a, c); } } vector visited(n + 1); function dfs; int phase = 1; dfs = [&](int idx, int prev) -> int { if (visited[idx] > 0) { return visited[idx] == phase; } visited[idx] = phase; for (auto e : vg[idx]) if (e.to != prev) { if (dfs(e.to, e.c == 1 ? idx : -1)) return 1; } return 0; }; for (int i : range(1, n + 1)) { phase = i; if (dfs(i, -1)) return 1; } return 0; } int main() { cout << fixed << setprecision(12); cout << (solve() ? "Yes" : "No") << endl; }