#include #include #include #include using namespace std; using Graph = vector>; // 探索 vector seen, finished; // サイクル復元のための情報 int pos = -1; // サイクル中に含まれる頂点 pos stack hist; // 訪問履歴 void dfs(const Graph &G, int v, int p) { seen[v] = true; hist.push(v); for (auto nv : G[v]) { if (nv == p) continue; // 逆流を禁止する // 完全終了した頂点はスルー if (finished[nv]) continue; // サイクルを検出 if (seen[nv] && !finished[nv]) { pos = nv; return; } // 再帰的に探索 dfs(G, nv, v); // サイクル検出したならば真っ直ぐに抜けていく if (pos != -1) return; } hist.pop(); finished[v] = true; } int main() { // 頂点数 (サイクルを一つ含むグラフなので辺数は N で確定) int N; cin >> N; int M; cin >> M; // グラフ入力受取 Graph G(N); for (int i = 0; i < M; ++i) { int a, b, c; cin >> a >> b >> c; --a; --b; // 頂点番号が 1-indexed で与えられるので 0-indexed にする G[a].push_back(b); if(c == 1) G[b].push_back(a); } // 探索 seen.assign(N, false); finished.assign(N, false); pos = -1; dfs(G, 0, -1); // サイクルを復元 set cycle; while (!hist.empty()) { int t = hist.top(); cycle.insert(t); hist.pop(); if (t == pos) break; } // クエリに答える for(int i = 0; i < N; i++){ if (cycle.count(i)){ cout << "Yes" << endl; return 0; } } cout << "No" << endl; return 0; }