結果

問題 No.1865 Make Cycle
ユーザー umezoumezo
提出日時 2022-03-04 23:43:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 237 ms / 3,000 ms
コード長 995 bytes
コンパイル時間 3,134 ms
コンパイル使用メモリ 203,516 KB
実行使用メモリ 12,616 KB
最終ジャッジ日時 2023-09-26 02:46:55
合計ジャッジ時間 7,192 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 143 ms
9,620 KB
testcase_01 AC 86 ms
8,204 KB
testcase_02 AC 166 ms
10,164 KB
testcase_03 AC 49 ms
9,080 KB
testcase_04 AC 99 ms
10,596 KB
testcase_05 AC 21 ms
7,104 KB
testcase_06 AC 135 ms
9,676 KB
testcase_07 AC 130 ms
9,444 KB
testcase_08 AC 178 ms
11,468 KB
testcase_09 AC 21 ms
7,240 KB
testcase_10 AC 142 ms
10,888 KB
testcase_11 AC 138 ms
10,288 KB
testcase_12 AC 121 ms
9,692 KB
testcase_13 AC 136 ms
9,756 KB
testcase_14 AC 93 ms
8,724 KB
testcase_15 AC 166 ms
10,440 KB
testcase_16 AC 181 ms
10,864 KB
testcase_17 AC 24 ms
6,432 KB
testcase_18 AC 21 ms
7,308 KB
testcase_19 AC 237 ms
12,616 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
 
#include<bits/stdc++.h>
using namespace std;

int color[100010];
bool flag=true;

void dfs(int u,vector<vector<int>>& G){
  color[u]=1;
  for(auto x:G[u]){
    if(color[x]==1){
      flag=false;
      return;
    }
    if(color[x]==0) dfs(x,G);
  }
  color[u]=2;
}
 
int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  int n,q;
  cin>>n>>q;
  vector<int> A(q),B(q);
  rep(i,q){
    cin>>A[i]>>B[i];
    A[i]--,B[i]--;
  }
  
  vector<vector<int>> G(n);
  rep(i,q) G[A[i]].push_back(B[i]);
  rep(i,n) if(color[i]==0) dfs(i,G);
  if(flag==true){
    cout<<-1<<endl;
    return 0;
  }
  
  int ok=q,ng=0;
  while(ok-ng>1){
    int mid=(ok+ng)/2;
    flag=true;
    rep(i,n) color[i]=0;
    vector<vector<int>> H(n);
    rep(i,mid) H[A[i]].push_back(B[i]);
    rep(i,n) if(color[i]==0) dfs(i,H);
    if(flag==true) ng=mid;
    else ok=mid;
  }
  cout<<ok<<endl;

  return 0;
}
0