結果

問題 No.1865 Make Cycle
ユーザー ayataka5ayataka5
提出日時 2022-03-04 23:19:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,297 bytes
コンパイル時間 1,129 ms
コンパイル使用メモリ 86,876 KB
実行使用メモリ 7,264 KB
最終ジャッジ日時 2023-09-26 02:35:21
合計ジャッジ時間 3,756 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 1 ms
4,376 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0