結果

問題 No.1865 Make Cycle
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2022-03-04 21:55:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 689 ms / 3,000 ms
コード長 828 bytes
コンパイル時間 257 ms
コンパイル使用メモリ 87,328 KB
実行使用メモリ 157,200 KB
最終ジャッジ日時 2023-09-26 00:50:28
合計ジャッジ時間 11,652 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 465 ms
120,288 KB
testcase_01 AC 332 ms
105,900 KB
testcase_02 AC 531 ms
129,624 KB
testcase_03 AC 261 ms
102,840 KB
testcase_04 AC 379 ms
126,244 KB
testcase_05 AC 464 ms
133,632 KB
testcase_06 AC 459 ms
127,964 KB
testcase_07 AC 398 ms
124,416 KB
testcase_08 AC 584 ms
143,920 KB
testcase_09 AC 489 ms
141,840 KB
testcase_10 AC 561 ms
142,584 KB
testcase_11 AC 519 ms
130,316 KB
testcase_12 AC 427 ms
122,060 KB
testcase_13 AC 453 ms
126,888 KB
testcase_14 AC 376 ms
117,124 KB
testcase_15 AC 526 ms
134,052 KB
testcase_16 AC 603 ms
138,976 KB
testcase_17 AC 408 ms
117,736 KB
testcase_18 AC 447 ms
133,248 KB
testcase_19 AC 689 ms
157,200 KB
testcase_20 AC 87 ms
71,708 KB
testcase_21 AC 88 ms
71,300 KB
testcase_22 AC 85 ms
71,672 KB
testcase_23 AC 86 ms
71,704 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""
1865:
二分探索でok

"""

import sys
from sys import stdin
from collections import deque

N,Q = map(int,stdin.readline().split())

AB = []
for i in range(Q):
    A,B = map(int,stdin.readline().split())
    A -= 1
    B -= 1
    AB.append( (A,B) )

L = 0
R = Q+1

while R-L != 1:

    M = (L+R)//2
    lis = [ [] for i in range(N) ]
    inlis = [0] * N

    for i in range(M):
        a,b = AB[i]
        lis[a].append(b)
        inlis[b] += 1

    q = deque([])

    for i in range(N):
        if inlis[i] == 0:
            q.append(i)

    end = 0
    while q:
        v = q.popleft()
        end += 1
        for nex in lis[v]:
            inlis[nex] -= 1
            if inlis[nex] == 0:
                q.append(nex)

    if end == N:
        L = M
    else:
        R = M

if R == Q+1:
    R = -1
print (R)
        
0