結果

問題 No.1865 Make Cycle
ユーザー shiomusubi496shiomusubi496
提出日時 2022-02-26 23:51:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,408 bytes
コンパイル時間 2,272 ms
コンパイル使用メモリ 208,968 KB
実行使用メモリ 7,328 KB
最終ジャッジ日時 2023-09-23 09:19:40
合計ジャッジ時間 7,317 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 140 ms
6,356 KB
testcase_01 AC 88 ms
5,776 KB
testcase_02 WA -
testcase_03 AC 56 ms
6,020 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 AC 127 ms
6,192 KB
testcase_08 AC 157 ms
7,328 KB
testcase_09 RE -
testcase_10 AC 161 ms
7,088 KB
testcase_11 RE -
testcase_12 AC 124 ms
6,484 KB
testcase_13 AC 129 ms
6,384 KB
testcase_14 RE -
testcase_15 AC 175 ms
6,840 KB
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 AC 1 ms
4,376 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 RE -
権限があれば一括ダウンロードができます

ソースコード

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, int k) {
        if (seen[v]) return true;
        if (visited[v]) return false;
        visited[v] = seen[v] = true;
        for (const auto& e : G[v]) {
            if (dfs(e, v)) 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, -1)) {
                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 < N; ++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