結果

問題 No.1865 Make Cycle
ユーザー SSRSSSRS
提出日時 2022-03-04 22:11:56
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 238 ms / 3,000 ms
コード長 887 bytes
コンパイル時間 2,221 ms
コンパイル使用メモリ 206,332 KB
実行使用メモリ 8,512 KB
最終ジャッジ日時 2023-09-26 01:16:11
合計ジャッジ時間 6,821 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 165 ms
6,648 KB
testcase_01 AC 101 ms
6,120 KB
testcase_02 AC 184 ms
6,836 KB
testcase_03 AC 64 ms
6,500 KB
testcase_04 AC 115 ms
7,240 KB
testcase_05 AC 151 ms
7,212 KB
testcase_06 AC 150 ms
7,104 KB
testcase_07 AC 138 ms
6,700 KB
testcase_08 AC 195 ms
7,772 KB
testcase_09 AC 143 ms
7,160 KB
testcase_10 AC 170 ms
7,408 KB
testcase_11 AC 156 ms
7,068 KB
testcase_12 AC 137 ms
6,544 KB
testcase_13 AC 157 ms
6,612 KB
testcase_14 AC 106 ms
6,332 KB
testcase_15 AC 179 ms
7,248 KB
testcase_16 AC 186 ms
7,536 KB
testcase_17 AC 138 ms
6,420 KB
testcase_18 AC 144 ms
7,436 KB
testcase_19 AC 238 ms
8,512 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  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]--;
  }
  int tv = Q + 1, fv = 0;
  while (tv - fv > 1){
    int mid = (tv + fv) / 2;
    vector<vector<int>> E(N);
    vector<int> d(N, 0);
    for (int i = 0; i < mid; i++){
      E[A[i]].push_back(B[i]);
      d[B[i]]++;
    }
    queue<int> q;
    for (int i = 0; i < N; i++){
      if (d[i] == 0){
        q.push(i);
      }
    }
    int cnt = 0;
    while (!q.empty()){
      int v = q.front();
      q.pop();
      cnt++;
      for (int w : E[v]){
        d[w]--;
        if (d[w] == 0){
          q.push(w);
        }
      }
    }
    if (cnt < N){
      tv = mid;
    } else {
      fv = mid;
    }
  }
  if (tv == Q + 1){
    cout << -1 << endl;
  } else {
    cout << tv << endl;
  }
}
0