結果

問題 No.1865 Make Cycle
ユーザー shiomusubi496shiomusubi496
提出日時 2022-03-04 21:05:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,162 bytes
コンパイル時間 2,310 ms
コンパイル使用メモリ 207,196 KB
実行使用メモリ 13,740 KB
最終ジャッジ日時 2023-09-25 22:51:52
合計ジャッジ時間 12,516 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

class GraphCycle {
  private:
    const vector<vector<int>>& G;
    std::vector<bool> visited, seen;
    bool result;
    bool dfs(int v) {
        if (seen[v]) return true;
        if (visited[v]) return false;
        visited[v] = seen[v] = true;
        for (const auto& e : G[v]) {
            if (dfs(e)) return true;
        }
        seen[v] = false;
        return false;
    }
    void init() {
        const int N = G.size();
        visited.assign(N, false);
        seen.assign(N, false);
        result = false;
        for (int i = 0; i < N; ++i) {
            if (dfs(i)) {
                result = true;
                break;
            }
        }
    }
  public:
    GraphCycle(const vector<vector<int>>& G) : G(G) { init(); }
    bool has_cycle() const { return result; }
};

int main() {
    int N, Q; cin >> N >> Q;
    vector<vector<int>> G(N);
    for (int i = 0; i < Q; ++i) {
        int a, b; cin >> a >> b;
        G[a - 1].push_back(b - 1);
        GraphCycle C(G);
        if (C.has_cycle()) {
            cout << i + 1 << endl;
            return 0;
        }
    }
    puts("-1");
}
0