結果

問題 No.1865 Make Cycle
ユーザー MtSakaMtSaka
提出日時 2022-02-26 08:05:53
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 2,054 ms / 3,000 ms
コード長 938 bytes
コンパイル時間 538 ms
コンパイル使用メモリ 10,924 KB
実行使用メモリ 37,816 KB
最終ジャッジ日時 2023-09-13 06:11:47
合計ジャッジ時間 31,137 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,212 ms
31,004 KB
testcase_01 AC 827 ms
25,304 KB
testcase_02 AC 1,420 ms
32,000 KB
testcase_03 AC 789 ms
28,804 KB
testcase_04 AC 1,293 ms
32,588 KB
testcase_05 AC 1,523 ms
31,392 KB
testcase_06 AC 1,331 ms
28,776 KB
testcase_07 AC 1,233 ms
29,892 KB
testcase_08 AC 1,653 ms
34,464 KB
testcase_09 AC 1,452 ms
30,280 KB
testcase_10 AC 1,598 ms
33,468 KB
testcase_11 AC 1,503 ms
31,204 KB
testcase_12 AC 1,209 ms
29,828 KB
testcase_13 AC 1,233 ms
30,828 KB
testcase_14 AC 1,046 ms
26,612 KB
testcase_15 AC 1,619 ms
32,032 KB
testcase_16 AC 1,726 ms
33,288 KB
testcase_17 AC 1,177 ms
27,304 KB
testcase_18 AC 1,542 ms
31,716 KB
testcase_19 AC 2,054 ms
37,816 KB
testcase_20 AC 15 ms
7,980 KB
testcase_21 AC 15 ms
7,792 KB
testcase_22 AC 15 ms
7,836 KB
testcase_23 AC 15 ms
7,848 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import setrecursionlimit


setrecursionlimit(10 ** 6)


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


if __name__ == '__main__':
    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, g, seen):
                has_cycle = True
    
        if has_cycle:
            right = mid
        else:
            left = mid
    
    ans = right
    if ans==q+1:
        ans=-1
    print(ans)
0