結果

問題 No.1865 Make Cycle
ユーザー KKT89KKT89
提出日時 2022-03-04 21:34:38
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 964 ms / 3,000 ms
コード長 3,209 bytes
コンパイル時間 2,264 ms
コンパイル使用メモリ 151,560 KB
実行使用メモリ 22,308 KB
最終ジャッジ日時 2023-09-26 00:13:00
合計ジャッジ時間 15,893 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 539 ms
15,208 KB
testcase_01 AC 334 ms
13,576 KB
testcase_02 AC 667 ms
17,024 KB
testcase_03 AC 161 ms
14,912 KB
testcase_04 AC 462 ms
18,404 KB
testcase_05 AC 605 ms
19,152 KB
testcase_06 AC 556 ms
17,060 KB
testcase_07 AC 453 ms
15,756 KB
testcase_08 AC 726 ms
19,456 KB
testcase_09 AC 635 ms
18,940 KB
testcase_10 AC 630 ms
18,888 KB
testcase_11 AC 614 ms
18,628 KB
testcase_12 AC 485 ms
16,164 KB
testcase_13 AC 525 ms
14,752 KB
testcase_14 AC 417 ms
14,816 KB
testcase_15 AC 734 ms
18,164 KB
testcase_16 AC 743 ms
19,716 KB
testcase_17 AC 510 ms
15,672 KB
testcase_18 AC 613 ms
20,128 KB
testcase_19 AC 964 ms
22,308 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

struct SCC{
    vector<vector<int> > gg,rg;
    vector<int> comp,order;
    vector<bool> used;
    vector<vector<int> > ng,vs;
    int n,nn;
    SCC(){}
    SCC(int v):gg(v),rg(v),comp(v,-1),used(v,0),n(v){}
    void add_edge(int x,int y){
        gg[x].push_back(y);
        rg[y].push_back(x);
    }
    int operator[](int k){
        return comp[k];
    }
    void dfs(int v){
        used[v]=1;
        for(int i:gg[v]){
            if(!used[i])dfs(i);
        }
        order.push_back(v);
    }
    void rdfs(int v,int k){
        used[v]=1;
        comp[v]=k;
        for(int i:rg[v]){
            if(!used[i])rdfs(i,k);
        }
    }
    int build(){
        for(int i=0;i<n;i++){
            if(!used[i])dfs(i);
        }
        for(int i=0;i<n;i++){
            used[i]=0;
        }
        int k=0;
        for(int i=order.size()-1;i>=0;i--){
            if(!used[order[i]])rdfs(order[i],k++);
        }
        nn=k;
        // それぞれの強連結成分に含まれる頂点の番号
        vs.resize(k,vector<int>());
        for(int i=0;i<n;i++){
            vs[comp[i]].push_back(i);
        }
        // 強連結成分をまとめた後のグラフ
        ng.resize(k,vector<int>());
        for(int i=0;i<n;i++){
            for(int j:gg[i]){
                if(comp[i]!=comp[j]){
                    ng[comp[i]].push_back(comp[j]);
                }
            }
        }
        for(int i=0;i<nn;i++){
            sort(ng[i].begin(),ng[i].end());
            ng[i].erase(unique(ng[i].begin(),ng[i].end()),ng[i].end());
        }
        return k;
    }
    int size(){
        return nn;
    }
    vector<vector<int> > graph(){
        return ng;
    }
    vector<int> vertices(int v){
        return vs[v];
    }
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n,m; cin >> n >> m;
    vector<int> x(m),y(m);
    for(int i=0;i<m;i++){
        cin >> x[i] >> y[i];
        x[i]--; y[i]--;
    }
    int l = 0, r = m;
    while(r-l>1){
        int mid = (l+r)/2;
        bool ok = false;
        SCC g(n);
        for(int i=0;i<=mid;i++){
            g.add_edge(x[i], y[i]);
        }
        int k = g.build();
        for(int i=0;i<k;i++){
            if(g.vs[i].size() > 1) ok = true;
        }
        if(ok){
            r = mid;
        }
        else{
            l = mid;
        }
    }
    if(r == m)r = -2;
    cout << r+1 << endl;
}
0