結果

問題 No.1865 Make Cycle
ユーザー ruthenruthen
提出日時 2022-03-04 22:00:22
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 235 ms / 3,000 ms
コード長 1,481 bytes
コンパイル時間 1,805 ms
コンパイル使用メモリ 173,520 KB
実行使用メモリ 9,356 KB
最終ジャッジ日時 2023-09-26 00:59:32
合計ジャッジ時間 6,219 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 149 ms
7,208 KB
testcase_01 AC 89 ms
6,336 KB
testcase_02 AC 165 ms
7,808 KB
testcase_03 AC 42 ms
6,744 KB
testcase_04 AC 104 ms
8,140 KB
testcase_05 AC 146 ms
8,124 KB
testcase_06 AC 143 ms
7,644 KB
testcase_07 AC 123 ms
7,312 KB
testcase_08 AC 178 ms
8,464 KB
testcase_09 AC 140 ms
8,232 KB
testcase_10 AC 161 ms
8,480 KB
testcase_11 AC 146 ms
8,124 KB
testcase_12 AC 124 ms
7,612 KB
testcase_13 AC 135 ms
6,860 KB
testcase_14 AC 99 ms
6,912 KB
testcase_15 AC 184 ms
7,912 KB
testcase_16 AC 190 ms
8,756 KB
testcase_17 AC 132 ms
6,964 KB
testcase_18 AC 148 ms
8,372 KB
testcase_19 AC 235 ms
9,356 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#ifdef _RUTHEN
#include "debug.hpp"
#else
#define show(...) true
#endif

vector<int> topological_sort(vector<vector<int>> &G) {
    int N = (int)G.size();
    vector<int> deg(N, 0);
    // 入次数をメモ
    for (int i = 0; i < N; i++) {
        for (int &v : G[i]) deg[v]++;
    }
    // 入次数0の点の集合を作成
    queue<int> que;
    for (int i = 0; i < N; i++) {
        if (deg[i] == 0) que.push(i);
    }
    vector<int> res;
    while (!que.empty()) {
        int v = que.front();
        que.pop();
        res.push_back(v);
        for (int &nx : G[v]) {
            // 入次数を減らす
            deg[nx]--;
            // 新たな入次数0の点を追加
            if (deg[nx] == 0) que.push(nx);
        }
    }
    return res;
}

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> using V = vector<T>;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N, Q;
    cin >> N >> Q;
    V<int> a(Q), b(Q);
    rep(i, Q) {
        cin >> a[i] >> b[i];
        a[i]--, b[i]--;
    }
    int ok = Q + 1, ng = 0;
    while (ok - ng > 1) {
        int md = (ok + ng) / 2;
        V<V<int>> G(N);
        rep(i, md) G[a[i]].push_back(b[i]);
        V<int> res = topological_sort(G);
        if (res.size() == N) {
            ng = md;
        } else {
            ok = md;
        }
    }
    cout << (ok == Q + 1 ? -1 : ok) << '\n';
    return 0;
}
0