結果

問題 No.1865 Make Cycle
ユーザー maguromaguro
提出日時 2022-03-04 22:17:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 323 ms / 3,000 ms
コード長 2,064 bytes
コンパイル時間 2,439 ms
コンパイル使用メモリ 209,556 KB
実行使用メモリ 9,568 KB
最終ジャッジ日時 2023-09-26 01:21:46
合計ジャッジ時間 7,481 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 197 ms
7,132 KB
testcase_01 AC 119 ms
6,496 KB
testcase_02 AC 222 ms
7,880 KB
testcase_03 AC 69 ms
6,720 KB
testcase_04 AC 142 ms
8,072 KB
testcase_05 AC 195 ms
8,188 KB
testcase_06 AC 185 ms
7,540 KB
testcase_07 AC 170 ms
7,396 KB
testcase_08 AC 240 ms
8,404 KB
testcase_09 AC 188 ms
8,116 KB
testcase_10 AC 210 ms
8,484 KB
testcase_11 AC 203 ms
7,996 KB
testcase_12 AC 173 ms
7,444 KB
testcase_13 AC 185 ms
6,960 KB
testcase_14 AC 130 ms
6,668 KB
testcase_15 AC 226 ms
7,896 KB
testcase_16 AC 265 ms
8,672 KB
testcase_17 AC 172 ms
7,168 KB
testcase_18 AC 187 ms
8,392 KB
testcase_19 AC 323 ms
9,568 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
constexpr int Inf = 2000000000;
constexpr long long INF= 2000000000000000000;

template<typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; }
template<typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; }

template<typename T,typename U>
T modpow(T N, U P, T M = -1) {
    if(P < 0) return 0;
    T ret = 1;
    if(M != -1) ret %= M;
    while(P) {
        if(P & 1) {
            if(M == -1) ret *= N;
            else ret = ret * N % M;
        }
        P /= 2;
        if(M == -1) N *= N;
        else N = N * N % M;
    }
    return ret;
}

constexpr long long MOD = 998244353;

//graphをトポロジカルソートする。返り値のvectorにトポロジカル順序が格納される
vector<int> topological_sort(int& n,vector<vector<int>>& graph) {
    vector<int> indegree(n);

    for(int i = 0;i < n;i++) {
        indegree[i] = 0;
    }
    for(auto x:graph) {
        for(auto y:x) {
            indegree[y]++;
        }
    }
    stack<int> st;
    for(int i = 0;i < n;i++) {
        if(indegree[i] == 0) {
            st.push(i);
        }
    }
    vector<int> res;
    while(!st.empty()) {
        int i = st.top();
        st.pop();
        res.push_back(i);
        for(auto x:graph[i]) {
            indegree[x]--;
            if(indegree[x] == 0) {
                st.push(x);
            }
        }
    }
    return res;
}

int main() {
    int n,q;
    cin >> n >> q;
    vector<pair<int,int>> vec(q);
    for(auto& [a,b]: vec) {
        cin >> a >> b;
        a--;
        b--;
    }

    int ng = -1;
    int ok = q;
    while(ok - ng > 1) {
        int mid = (ng + ok) / 2;
        vector<vector<int>> graph(n);
        for(int i = 0;i <= mid;i++) {
            graph[vec[i].first].push_back(vec[i].second);
        }
        vector<int> res = topological_sort(n,graph);
        if(res.size() == n) ng = mid;
        else ok = mid;
    }

    if(ok == q) cout << -1 << endl;
    else cout << ok + 1 << endl;
}
0