結果

問題 No.1865 Make Cycle
ユーザー rlangevin
提出日時 2023-03-31 08:59:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,971 ms / 3,000 ms
コード長 1,066 bytes
コンパイル時間 324 ms
コンパイル使用メモリ 82,404 KB
実行使用メモリ 274,868 KB
最終ジャッジ日時 2024-11-18 15:49:44
合計ジャッジ時間 25,634 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

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