結果
問題 |
No.1865 Make Cycle
|
ユーザー |
![]() |
提出日時 | 2022-03-05 00:50:06 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 631 ms / 3,000 ms |
コード長 | 744 bytes |
コンパイル時間 | 394 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 184,452 KB |
最終ジャッジ日時 | 2024-07-18 23:55:28 |
合計ジャッジ時間 | 10,628 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 20 |
ソースコード
from collections import deque, defaultdict n, Q = map(int,input().split()) query = [] for i in range(Q): a, b = map(int,input().split()) query.append((a-1, b-1)) def check_cycle(r): ins = [0 for i in range(n)] outs = [[] for i in range(n)] for a, b in query[:r]: ins[b] += 1 outs[a].append(b) res = [] mada = deque() for i in range(n): if ins[i] == 0: mada.append(i) while len(mada) > 0: i = mada.popleft() res.append(i) for j in outs[i]: ins[j] -= 1 if ins[j] == 0: mada.append(j) if len(res) != n: return True return False suki = Q + 1 kirai = 0 while suki - kirai > 1: targ = (suki + kirai) // 2 if check_cycle(targ): suki = targ else: kirai = targ if suki > Q: print(-1) else: print(suki)