結果

問題 No.1865 Make Cycle
ユーザー milanis48663220milanis48663220
提出日時 2022-03-04 21:42:20
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 413 ms / 3,000 ms
コード長 2,516 bytes
コンパイル時間 1,307 ms
コンパイル使用メモリ 126,784 KB
実行使用メモリ 13,076 KB
最終ジャッジ日時 2023-09-26 00:27:29
合計ジャッジ時間 6,759 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 229 ms
10,340 KB
testcase_01 AC 152 ms
8,996 KB
testcase_02 AC 268 ms
10,828 KB
testcase_03 AC 90 ms
10,052 KB
testcase_04 AC 197 ms
11,504 KB
testcase_05 AC 31 ms
11,036 KB
testcase_06 AC 254 ms
10,180 KB
testcase_07 AC 221 ms
10,080 KB
testcase_08 AC 296 ms
11,748 KB
testcase_09 AC 28 ms
10,844 KB
testcase_10 AC 226 ms
11,508 KB
testcase_11 AC 265 ms
11,000 KB
testcase_12 AC 193 ms
10,208 KB
testcase_13 AC 212 ms
10,392 KB
testcase_14 AC 169 ms
9,272 KB
testcase_15 AC 263 ms
10,964 KB
testcase_16 AC 319 ms
11,492 KB
testcase_17 AC 26 ms
9,444 KB
testcase_18 AC 28 ms
11,296 KB
testcase_19 AC 413 ms
13,076 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

bool topological_sort(vector<vector<int>> g, vector<int> &order){
    int n = g.size();
    order.clear();
    vector<bool> used(n, false);
    function<void(int)> dfs = [&](int v){
        used[v] = true;
        for(int to : g[v]){
            if(!used[to]) dfs(to);
        }
        order.push_back(v);
    };
    for(int v = 0; v < n; v++){
        if(!used[v]) dfs(v);
    }
    reverse(order.begin(), order.end());
    vector<int> inv_order(n);
    for(int i = 0; i < n; i++) inv_order[order[i]] = i;
    for(int v = 0; v < n; v++){
        for(int u : g[v]){
            if(inv_order[v] > inv_order[u]) return false;
        }
    }
    return true;
}


int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, q; cin >> n >> q;
    vector<int> a(q), b(q);
    for(int i = 0; i < q; i++){
        cin >> a[i] >> b[i]; a[i]--; b[i]--;
    }
    auto judge = [&](int x){
        vector<vector<int>> g(n);
        for(int i = 0; i < x; i++){
            g[a[i]].push_back(b[i]);
        }
        vector<int> ord;
        return !topological_sort(g, ord);
    };
    if(!judge(q)){
        cout << -1 << endl;
        return 0;
    }
    int l = 0, r = q;
    while(r-l > 1){
        int x = (l+r)/2;
        if(judge(x)) r = x;
        else l = x;
    }
    cout << r << endl;
}
0