結果

問題 No.1865 Make Cycle
ユーザー MtSakaMtSaka
提出日時 2022-02-28 01:01:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,272 ms / 3,000 ms
コード長 955 bytes
コンパイル時間 1,352 ms
コンパイル使用メモリ 86,820 KB
実行使用メモリ 217,268 KB
最終ジャッジ日時 2023-09-25 22:55:35
合計ジャッジ時間 19,676 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 818 ms
150,816 KB
testcase_01 AC 591 ms
130,492 KB
testcase_02 AC 1,005 ms
179,700 KB
testcase_03 AC 297 ms
103,092 KB
testcase_04 AC 599 ms
166,108 KB
testcase_05 AC 842 ms
188,016 KB
testcase_06 AC 1,024 ms
173,024 KB
testcase_07 AC 744 ms
159,704 KB
testcase_08 AC 902 ms
180,160 KB
testcase_09 AC 775 ms
186,992 KB
testcase_10 AC 913 ms
189,868 KB
testcase_11 AC 906 ms
185,072 KB
testcase_12 AC 660 ms
142,748 KB
testcase_13 AC 752 ms
149,408 KB
testcase_14 AC 653 ms
144,384 KB
testcase_15 AC 1,080 ms
185,852 KB
testcase_16 AC 1,095 ms
192,536 KB
testcase_17 AC 826 ms
170,196 KB
testcase_18 AC 829 ms
192,232 KB
testcase_19 AC 1,272 ms
217,268 KB
testcase_20 AC 75 ms
71,364 KB
testcase_21 AC 73 ms
71,304 KB
testcase_22 AC 76 ms
71,144 KB
testcase_23 AC 74 ms
71,348 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 right - left > 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