結果
問題 |
No.1865 Make Cycle
|
ユーザー |
![]() |
提出日時 | 2022-11-13 02:32:56 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 614 ms / 3,000 ms |
コード長 | 927 bytes |
コンパイル時間 | 787 ms |
コンパイル使用メモリ | 82,008 KB |
実行使用メモリ | 150,640 KB |
最終ジャッジ日時 | 2024-09-14 13:52:47 |
合計ジャッジ時間 | 10,848 ms |
ジャッジサーバーID (参考情報) |
judge6 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 20 |
ソースコード
from collections import deque class DetectDAG: # REQUIRED # from collections import deque def __init__(self, n): self.n = n self.ikeru = [[] for i in range(n)] self.deg = [0] * n def add_edge(self, x, y): self.ikeru[x].append(y) self.deg[y] += 1 def isdag(self): mada = deque() for i in range(self.n): if self.deg[i] == 0: mada.append(i) while mada: i = mada.popleft() for j in self.ikeru[i]: self.deg[j] -= 1 if self.deg[j] == 0: mada.append(j) for i in self.deg: if i > 0: return False return True n, q = map(int,input().split()) x = [0] * q y = [0] * q for i in range(q): a, b = map(int,input().split()) x[i] = a-1 y[i] = b-1 suki = q kirai = -1 while suki - kirai > 1: targ = (suki + kirai) // 2 g = DetectDAG(n) for i in range(targ): g.add_edge(x[i], y[i]) if not g.isdag(): suki = targ else: kirai = targ if suki == q: print(-1) else: print(suki)