結果

問題 No.1865 Make Cycle
ユーザー MtSakaMtSaka
提出日時 2022-02-26 08:03:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,420 ms / 3,000 ms
コード長 942 bytes
コンパイル時間 296 ms
コンパイル使用メモリ 87,080 KB
実行使用メモリ 220,916 KB
最終ジャッジ日時 2023-09-23 09:19:16
合計ジャッジ時間 20,460 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 731 ms
171,252 KB
testcase_01 AC 658 ms
145,200 KB
testcase_02 AC 1,073 ms
185,492 KB
testcase_03 AC 338 ms
107,420 KB
testcase_04 AC 587 ms
170,688 KB
testcase_05 AC 763 ms
187,816 KB
testcase_06 AC 1,051 ms
178,552 KB
testcase_07 AC 855 ms
176,256 KB
testcase_08 AC 1,186 ms
196,772 KB
testcase_09 AC 733 ms
186,916 KB
testcase_10 AC 915 ms
189,716 KB
testcase_11 AC 944 ms
184,764 KB
testcase_12 AC 829 ms
169,076 KB
testcase_13 AC 788 ms
173,412 KB
testcase_14 AC 682 ms
157,508 KB
testcase_15 AC 1,252 ms
196,656 KB
testcase_16 AC 1,340 ms
201,340 KB
testcase_17 AC 777 ms
169,764 KB
testcase_18 AC 787 ms
191,808 KB
testcase_19 AC 1,420 ms
220,916 KB
testcase_20 AC 71 ms
71,148 KB
testcase_21 AC 71 ms
70,852 KB
testcase_22 AC 72 ms
71,036 KB
testcase_23 AC 71 ms
71,012 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