結果

問題 No.1865 Make Cycle
ユーザー MtSakaMtSaka
提出日時 2022-02-28 01:00:52
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 955 bytes
コンパイル時間 1,282 ms
コンパイル使用メモリ 86,996 KB
実行使用メモリ 82,488 KB
最終ジャッジ日時 2023-09-23 09:21:14
合計ジャッジ時間 4,668 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 126 ms
79,952 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 127 ms
80,300 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 127 ms
80,044 KB
testcase_18 AC 125 ms
80,120 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 73 ms
71,012 KB
testcase_22 WA -
testcase_23 AC 72 ms
71,092 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 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
                break
    
        if has_cycle:
            right = mid
        else:
            left = mid
    
    ans = right
    if ans==q+1:
        ans=-1
    print(ans)
0