結果
問題 |
No.1865 Make Cycle
|
ユーザー |
|
提出日時 | 2022-03-04 23:19:51 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,297 bytes |
コンパイル時間 | 1,044 ms |
コンパイル使用メモリ | 84,616 KB |
最終ジャッジ日時 | 2025-01-28 06:27:45 |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 WA * 3 |
other | WA * 20 |
ソースコード
#include <iostream> #include <vector> #include <set> #include <stack> using namespace std; using Graph = vector<vector<int>>; // 探索 vector<bool> seen, finished; bool ans = false; void dfs(const Graph &G, int v, int p) { seen[v] = true; for (auto nv : G[v]) { if (nv == p) continue; // 逆流を禁止する // 完全終了した頂点はスルー if (finished[nv]) continue; // サイクルを検出 if (seen[nv] && !finished[nv]) { ans = true; return; } // 再帰的に探索 dfs(G, nv, v); // サイクル検出したならば真っ直ぐに抜けていく if (ans) return; } finished[v] = true; } int main() { // 頂点数 (サイクルを一つ含むグラフなので辺数は N で確定) int N; cin >> N; int Q; cin >> Q; // グラフ入力受取 Graph G(N); for (int i = 0; i < Q; ++i) { int a, b; cin >> a >> b; --a, --b; // 頂点番号が 1-indexed で与えられるので 0-indexed にする G[a].push_back(b); seen.assign(N, false), finished.assign(N, false); dfs(G, 0, -1); if(ans) { cout << i+1 << endl; return 0; } } return 0; }