結果

問題 No.1865 Make Cycle
ユーザー NaHCO314NaHCO314
提出日時 2022-02-25 19:18:54
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 2,375 ms / 3,000 ms
コード長 767 bytes
コンパイル時間 78 ms
コンパイル使用メモリ 10,920 KB
実行使用メモリ 37,816 KB
最終ジャッジ日時 2023-09-23 09:17:14
合計ジャッジ時間 31,966 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,363 ms
30,808 KB
testcase_01 AC 940 ms
25,396 KB
testcase_02 AC 1,532 ms
32,044 KB
testcase_03 AC 814 ms
28,724 KB
testcase_04 AC 1,399 ms
32,424 KB
testcase_05 AC 1,544 ms
31,316 KB
testcase_06 AC 1,373 ms
28,836 KB
testcase_07 AC 1,289 ms
29,776 KB
testcase_08 AC 1,790 ms
34,436 KB
testcase_09 AC 1,515 ms
30,328 KB
testcase_10 AC 1,623 ms
33,396 KB
testcase_11 AC 1,558 ms
31,224 KB
testcase_12 AC 1,331 ms
29,800 KB
testcase_13 AC 1,323 ms
30,792 KB
testcase_14 AC 1,052 ms
26,612 KB
testcase_15 AC 1,691 ms
32,012 KB
testcase_16 AC 1,812 ms
33,104 KB
testcase_17 AC 1,216 ms
27,412 KB
testcase_18 AC 1,613 ms
31,656 KB
testcase_19 AC 2,375 ms
37,816 KB
testcase_20 AC 16 ms
7,888 KB
testcase_21 AC 17 ms
7,764 KB
testcase_22 AC 17 ms
7,968 KB
testcase_23 AC 16 ms
7,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import setrecursionlimit


setrecursionlimit(10 ** 6)


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