結果

問題 No.1865 Make Cycle
ユーザー shiomusubi496shiomusubi496
提出日時 2022-02-26 23:57:10
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 223 ms / 3,000 ms
コード長 1,394 bytes
コンパイル時間 2,366 ms
コンパイル使用メモリ 209,904 KB
実行使用メモリ 8,164 KB
最終ジャッジ日時 2023-09-23 09:18:12
合計ジャッジ時間 6,816 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 152 ms
6,472 KB
testcase_01 AC 88 ms
5,532 KB
testcase_02 AC 170 ms
6,376 KB
testcase_03 AC 57 ms
6,256 KB
testcase_04 AC 106 ms
6,484 KB
testcase_05 AC 155 ms
7,060 KB
testcase_06 AC 147 ms
6,668 KB
testcase_07 AC 132 ms
6,220 KB
testcase_08 AC 158 ms
7,284 KB
testcase_09 AC 147 ms
6,688 KB
testcase_10 AC 164 ms
6,980 KB
testcase_11 AC 152 ms
6,956 KB
testcase_12 AC 124 ms
6,488 KB
testcase_13 AC 144 ms
6,588 KB
testcase_14 AC 102 ms
5,828 KB
testcase_15 AC 177 ms
6,972 KB
testcase_16 AC 183 ms
7,116 KB
testcase_17 AC 150 ms
6,208 KB
testcase_18 AC 153 ms
6,904 KB
testcase_19 AC 223 ms
8,164 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

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<pair<int, int>> A(Q);
    for (int i = 0; i < Q; ++i) {
        cin >> A[i].first >> A[i].second;
        --A[i].first; --A[i].second;
    }
    int ok = Q + 1, ng = 0;
    while (ok - ng > 1) {
        int mid = (ok + ng) >> 1;
        vector<vector<int>> G(N);
        for (int i = 0; i < mid; ++i) G[A[i].first].push_back(A[i].second);
        GraphCycle cyc(G);
        if (cyc.has_cycle()) ok = mid;
        else ng = mid;
    }
    if (ok == Q + 1) puts("-1");
    else cout << ok << endl;
}
0