結果

問題 No.1865 Make Cycle
ユーザー 👑 tamatotamato
提出日時 2022-03-04 21:49:14
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 811 ms / 3,000 ms
コード長 1,144 bytes
コンパイル時間 604 ms
コンパイル使用メモリ 87,244 KB
実行使用メモリ 165,820 KB
最終ジャッジ日時 2023-09-26 00:37:59
合計ジャッジ時間 13,178 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 525 ms
127,048 KB
testcase_01 AC 329 ms
104,032 KB
testcase_02 AC 589 ms
137,520 KB
testcase_03 AC 256 ms
110,520 KB
testcase_04 AC 400 ms
133,872 KB
testcase_05 AC 552 ms
141,724 KB
testcase_06 AC 506 ms
134,804 KB
testcase_07 AC 459 ms
125,872 KB
testcase_08 AC 599 ms
144,892 KB
testcase_09 AC 471 ms
131,888 KB
testcase_10 AC 526 ms
135,604 KB
testcase_11 AC 555 ms
133,804 KB
testcase_12 AC 440 ms
126,600 KB
testcase_13 AC 472 ms
125,032 KB
testcase_14 AC 355 ms
115,132 KB
testcase_15 AC 642 ms
141,388 KB
testcase_16 AC 662 ms
150,012 KB
testcase_17 AC 462 ms
122,124 KB
testcase_18 AC 510 ms
143,300 KB
testcase_19 AC 811 ms
165,820 KB
testcase_20 AC 99 ms
71,828 KB
testcase_21 AC 97 ms
72,020 KB
testcase_22 AC 104 ms
71,824 KB
testcase_23 AC 101 ms
72,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    from collections import deque
    input = sys.stdin.buffer.readline

    N, Q = map(int, input().split())
    AB = [(0, 0)]
    for _ in range(Q):
        AB.append(tuple(map(int, input().split())))

    ok = Q + 1
    ng = 0
    mid = (ok + ng) // 2
    while ok - ng > 1:
        adj = [[] for _ in range(N + 1)]
        in_num = [0] * (N + 1)
        for a, b in AB[1:mid+1]:
            adj[a].append(b)
            in_num[b] += 1
        que = deque()
        seen = [0] * (N + 1)
        for v in range(1, N + 1):
            if in_num[v] == 0:
                seen[v] = 1
                que.append(v)
        while que:
            v = que.popleft()
            for u in adj[v]:
                in_num[u] -= 1
                if in_num[u] == 0:
                    if not seen[u]:
                        seen[u] = 1
                        que.append(u)
        if sum(seen) == N:
            ng = mid
        else:
            ok = mid
        mid = (ok + ng) // 2
    if ok == Q + 1:
        print(-1)
    else:
        print(ok)


if __name__ == '__main__':
    main()
0