結果

問題 No.1865 Make Cycle
ユーザー NaHCO314NaHCO314
提出日時 2022-02-25 18:24:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,049 ms / 3,000 ms
コード長 834 bytes
コンパイル時間 283 ms
コンパイル使用メモリ 87,112 KB
実行使用メモリ 216,524 KB
最終ジャッジ日時 2023-09-23 09:17:32
合計ジャッジ時間 17,280 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 676 ms
168,388 KB
testcase_01 AC 490 ms
141,936 KB
testcase_02 AC 787 ms
182,868 KB
testcase_03 AC 431 ms
131,532 KB
testcase_04 AC 641 ms
171,688 KB
testcase_05 AC 740 ms
187,856 KB
testcase_06 AC 737 ms
175,680 KB
testcase_07 AC 691 ms
171,472 KB
testcase_08 AC 878 ms
191,412 KB
testcase_09 AC 702 ms
185,492 KB
testcase_10 AC 799 ms
188,764 KB
testcase_11 AC 776 ms
183,200 KB
testcase_12 AC 667 ms
167,124 KB
testcase_13 AC 691 ms
168,432 KB
testcase_14 AC 578 ms
153,828 KB
testcase_15 AC 844 ms
190,588 KB
testcase_16 AC 895 ms
196,092 KB
testcase_17 AC 636 ms
169,328 KB
testcase_18 AC 756 ms
191,424 KB
testcase_19 AC 1,049 ms
216,524 KB
testcase_20 AC 76 ms
71,144 KB
testcase_21 AC 75 ms
71,268 KB
testcase_22 AC 74 ms
71,384 KB
testcase_23 AC 76 ms
71,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import setrecursionlimit
from pypyjit import set_param


setrecursionlimit(10 ** 6)
set_param("max_unroll_recursion=-1")


def DFS(v):
    seen[v] = 1
    for i in g[v]:
        if seen[i] == 1 or (seen[i] == 0 and DFS(i)):
            return True
    seen[v] = 2
    return False


n, q = map(int, input().split())
edges = []
for i in range(q):
    a, b = map(int, input().split())
    edges.append((a-1, b-1))
left, right = -1, q + 1

while abs(left - right) > 1:
    mid = (right + left) // 2

    g = [[] for _ in range(n)]
    for a, b in edges[:mid]:
        g[a].append(b)
    seen = [0] * n
    has_cycle = False
    for i in range(n):
        if seen[i] == 0 and DFS(i):
            has_cycle = True

    if has_cycle:
        right = mid
    else:
        left = mid

ans = right
print(ans if ans != q + 1 else -1)
0