結果

問題 No.1865 Make Cycle
ユーザー fumofumofunifumofumofuni
提出日時 2022-03-04 22:36:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 258 ms / 3,000 ms
コード長 1,793 bytes
コンパイル時間 2,180 ms
コンパイル使用メモリ 208,780 KB
実行使用メモリ 11,592 KB
最終ジャッジ日時 2023-09-26 01:44:16
合計ジャッジ時間 7,504 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 182 ms
8,364 KB
testcase_01 AC 111 ms
7,656 KB
testcase_02 AC 199 ms
9,576 KB
testcase_03 AC 67 ms
8,176 KB
testcase_04 AC 161 ms
9,984 KB
testcase_05 AC 169 ms
10,148 KB
testcase_06 AC 167 ms
9,364 KB
testcase_07 AC 152 ms
8,804 KB
testcase_08 AC 210 ms
10,528 KB
testcase_09 AC 160 ms
10,284 KB
testcase_10 AC 188 ms
10,400 KB
testcase_11 AC 173 ms
10,048 KB
testcase_12 AC 154 ms
9,276 KB
testcase_13 AC 170 ms
8,844 KB
testcase_14 AC 121 ms
8,028 KB
testcase_15 AC 203 ms
9,932 KB
testcase_16 AC 214 ms
10,528 KB
testcase_17 AC 157 ms
8,260 KB
testcase_18 AC 167 ms
10,660 KB
testcase_19 AC 258 ms
11,592 KB
testcase_20 AC 1 ms
4,384 KB
testcase_21 AC 1 ms
4,384 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e17;
const ll dy[9]={0,-1,0,1,1,1,-1,-1,0};
const ll dx[9]={-1,0,1,0,1,-1,1,-1,0};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
vl topsort(vvl &g){
    ll n=g.size();
    vl ans,ind(n);
    rep(i,n)for(auto e:g[i])ind[e]++;
    queue<ll> que;
    rep(i,n)if(!ind[i])que.push(i);
    while(!que.empty()){
        ll v=que.front();que.pop();
        ans.pb(v);
        for(auto nv:g[v]){
            ind[nv]--;if(!ind[nv])que.push(nv);
        }
    }
    return ans;
}


int main(){
    ll n,q;cin >> n >> q;
    vl a(q),b(q);
    rep(i,q){
        cin >> a[i] >> b[i];a[i]--;b[i]--;
    }
    ll ok=q+1,ng=0;
    while(ok-ng>1){
        ll mid=(ok+ng)/2;
        vvl g(n);
        rep(i,mid)g[a[i]].emplace_back(b[i]);
        ll q=topsort(g).size();
        if(q!=n)ok=mid;
        else ng=mid;
    }
    if(ok>q)cout << -1 << endl;
    else cout << ok << endl;
}
0