結果

問題 No.1865 Make Cycle
ユーザー MtSakaMtSaka
提出日時 2022-02-26 08:01:22
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 210 ms / 3,000 ms
コード長 1,072 bytes
コンパイル時間 2,055 ms
コンパイル使用メモリ 205,556 KB
実行使用メモリ 8,224 KB
最終ジャッジ日時 2023-09-23 09:16:41
合計ジャッジ時間 6,226 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 139 ms
6,572 KB
testcase_01 AC 86 ms
5,876 KB
testcase_02 AC 165 ms
6,796 KB
testcase_03 AC 55 ms
6,296 KB
testcase_04 AC 102 ms
6,960 KB
testcase_05 AC 143 ms
7,304 KB
testcase_06 AC 138 ms
6,760 KB
testcase_07 AC 130 ms
6,424 KB
testcase_08 AC 161 ms
7,664 KB
testcase_09 AC 138 ms
7,304 KB
testcase_10 AC 155 ms
7,332 KB
testcase_11 AC 142 ms
7,156 KB
testcase_12 AC 120 ms
6,796 KB
testcase_13 AC 140 ms
6,564 KB
testcase_14 AC 96 ms
6,000 KB
testcase_15 AC 169 ms
7,088 KB
testcase_16 AC 174 ms
7,648 KB
testcase_17 AC 139 ms
6,584 KB
testcase_18 AC 141 ms
7,416 KB
testcase_19 AC 210 ms
8,224 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
bool dfs(int i, vector<int>& seen, vector<vector<int> >& g)
{
    seen[i] = 1;
    for (auto& j : g[i]) {
        if (seen[j] == 1 || (seen[j] == 0 && dfs(j, seen, g))) {
            return true;
        }
    }
    seen[i] = 2;
    return false;
}
int main()
{
    int n, q;
    cin >> n >> q;
    vector<pair<int, int> > edges(q);
    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b;
        edges[i] = { a - 1, b - 1 };
    }
    int l = -1, r = q + 1;
    while (r - l > 1) {
        int mid = (l + r) / 2;
        vector<vector<int> > g(n);
        for (int i = 0; i < mid; i++)
            g[edges[i].first].push_back(edges[i].second);
        vector<int> seen(n, 0);
        bool has_cycle = false;
        for (int i = 0; i < n; i++) {
            if (seen[i] == 0 && dfs(i, seen, g)) {
                has_cycle = true;
                break;
            }
        }
        if (has_cycle)
            r = mid;
        else
            l = mid;
    }
    cout << (r == q + 1 ? -1 : r) << endl;
}
0