#include #include #include #include #include #define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i) #define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i) #define all(x) (x).begin(), (x).end() using namespace std; using ll = long long; template inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return 0;} template inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return 0;} class UnionFind { private: vector parent_; vector node_rank_; vector sizes_; public: UnionFind(int node_num): parent_(vector(node_num)), node_rank_(vector(node_num)), sizes_(vector(node_num)) { for (int i = 0; i < node_num; ++i) { parent_[i] = i; node_rank_[i] = 0; sizes_[i] = 1; } } int getRoot(int u) { return parent_[u] == u ? u : parent_[u] = getRoot(parent_[u]); } bool isSame(int u, int v) { return getRoot(u) == getRoot(v); } void unite(int u, int v) { u = getRoot(u); v = getRoot(v); if (u == v) return; if (node_rank_[u] < node_rank_[v]) { parent_[u] = v; sizes_[v] += sizes_[u]; } else { parent_[v] = u; sizes_[u] += sizes_[v]; if (node_rank_[u] == node_rank_[v]) { node_rank_[u]++; } } } int getSize(int u) { return sizes_[getRoot(u)]; } }; void dfs(const vector>& graph, vector& seen, vector& finished, int node, bool& pos) { seen[node] = true; for (auto& next_node : graph[node]) { if (finished[next_node]) { continue; } if (seen[next_node] && !finished[next_node]) { pos = true; return; } dfs(graph, seen, finished, next_node, pos); if (pos) { return; } } finished[node] = true; } int main() { cin.tie(0); ios::sync_with_stdio(false); int N, M; cin >> N >> M; vector A(M), B(M), C(M); rep(i, 0, M) { cin >> A[i] >> B[i] >> C[i]; --A[i], --B[i]; } UnionFind uf(N); rep(i, 0, M) { if (C[i] == 1) { uf.unite(A[i], B[i]); } } set S; rep(i, 0, N) { S.insert(uf.getRoot(i)); } map node_map; int node = 0; for (auto s : S) { node_map[s] = node++; } vector> graph(node); rep(i, 0, M) { if (C[i] == 2) { int u = node_map[uf.getRoot(A[i])]; int v = node_map[uf.getRoot(B[i])]; graph[u].push_back(v); } } vector seen(node, false), finished(node, false); rep(i, 0, node) { if (!finished[i]) { bool pos = false; dfs(graph, seen, finished, i, pos); if (pos) { cout << "Yes" << endl; return 0; } } } cout << "No" << endl; return 0; }