結果

問題 No.1865 Make Cycle
ユーザー rlangevinrlangevin
提出日時 2023-03-31 08:59:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,606 ms / 3,000 ms
コード長 1,066 bytes
コンパイル時間 179 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 275,180 KB
最終ジャッジ日時 2024-04-29 12:39:37
合計ジャッジ時間 33,345 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,668 ms
234,736 KB
testcase_01 AC 1,216 ms
184,528 KB
testcase_02 AC 1,985 ms
261,964 KB
testcase_03 AC 969 ms
179,084 KB
testcase_04 AC 1,696 ms
247,392 KB
testcase_05 AC 250 ms
93,184 KB
testcase_06 AC 1,830 ms
254,200 KB
testcase_07 AC 1,661 ms
246,740 KB
testcase_08 AC 2,121 ms
268,364 KB
testcase_09 AC 247 ms
93,056 KB
testcase_10 AC 1,886 ms
264,660 KB
testcase_11 AC 1,864 ms
265,676 KB
testcase_12 AC 1,616 ms
238,456 KB
testcase_13 AC 1,570 ms
233,260 KB
testcase_14 AC 1,400 ms
206,004 KB
testcase_15 AC 2,033 ms
269,460 KB
testcase_16 AC 2,186 ms
268,112 KB
testcase_17 AC 231 ms
92,800 KB
testcase_18 AC 254 ms
93,968 KB
testcase_19 AC 2,606 ms
275,180 KB
testcase_20 AC 44 ms
52,480 KB
testcase_21 AC 44 ms
52,608 KB
testcase_22 AC 45 ms
52,352 KB
testcase_23 AC 44 ms
52,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline = sys.stdin.readline

from heapq import *
def topsort(G):
    Q = []
    count = [0] * len(G)
 
    for u in range(len(G)):
        for v in G[u]:
            count[v] += 1
 
    for u in range(len(G)):
        if count[u] == 0:
            heappush(Q, u)
 
    anslst = []
    while Q:
        u = heappop(Q)
        anslst.append(u)
        temp = []
        for v in G[u]:
            count[v] -= 1
            if count[v] == 0:
                heappush(Q, v)
 
    anslst.reverse()
    return anslst


def check(m):
    G = [[] for i in range(N)]
    S = set()
    for i in range(m):
        v = A[i] * (N + 5) + B[i]
        if v not in S:
            G[A[i] - 1].append(B[i] - 1)
            S.add(v)

    return len(topsort(G)) == N


N, Q = map(int, readline().split())
A, B = [None] * Q, [None] * Q
for i in range(Q):
    A[i], B[i] = map(int, readline().split())

if check(Q):
    print(-1)
    exit()

yes = 0
no = Q
while no - yes != 1:
    mid = (yes + no)//2
    if check(mid):
        yes = mid
    else:
        no = mid
print(no)
0