結果

問題 No.1865 Make Cycle
ユーザー NaHCO314NaHCO314
提出日時 2022-02-25 19:22:22
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,025 ms / 3,000 ms
コード長 1,049 bytes
コンパイル時間 576 ms
コンパイル使用メモリ 87,028 KB
実行使用メモリ 215,380 KB
最終ジャッジ日時 2023-09-23 09:16:14
合計ジャッジ時間 17,463 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 650 ms
167,476 KB
testcase_01 AC 444 ms
140,404 KB
testcase_02 AC 679 ms
181,288 KB
testcase_03 AC 370 ms
131,080 KB
testcase_04 AC 574 ms
170,864 KB
testcase_05 AC 690 ms
187,868 KB
testcase_06 AC 668 ms
174,988 KB
testcase_07 AC 611 ms
171,712 KB
testcase_08 AC 816 ms
189,780 KB
testcase_09 AC 649 ms
185,272 KB
testcase_10 AC 722 ms
188,360 KB
testcase_11 AC 726 ms
182,724 KB
testcase_12 AC 618 ms
166,212 KB
testcase_13 AC 648 ms
166,916 KB
testcase_14 AC 523 ms
153,648 KB
testcase_15 AC 749 ms
190,316 KB
testcase_16 AC 814 ms
194,792 KB
testcase_17 AC 618 ms
168,956 KB
testcase_18 AC 717 ms
190,788 KB
testcase_19 AC 1,025 ms
215,380 KB
testcase_20 AC 71 ms
71,452 KB
testcase_21 AC 70 ms
71,452 KB
testcase_22 AC 69 ms
71,648 KB
testcase_23 AC 69 ms
71,612 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import setrecursionlimit
from pypyjit import set_param


setrecursionlimit(10 ** 6)
set_param("max_unroll_recursion=-1")


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


def main(n, q, edges):
    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
    return ans if ans != q + 1 else -1


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))

    ans = main(n, q, edges)
    print(ans)
0