結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 122 ms
79,848 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 119 ms
79,968 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 124 ms
80,444 KB
testcase_18 AC 118 ms
80,180 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 70 ms
71,028 KB
testcase_22 WA -
testcase_23 AC 70 ms
70,896 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
    
        if has_cycle:
            right = mid
        else:
            left = mid
    
    ans = right
    if ans==q+1:
        ans=-1
    print(ans)
0