結果

問題 No.1865 Make Cycle
ユーザー DrDrpilotDrDrpilot
提出日時 2022-03-08 16:59:01
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,403 ms / 3,000 ms
コード長 654 bytes
コンパイル時間 176 ms
コンパイル使用メモリ 10,764 KB
実行使用メモリ 32,652 KB
最終ジャッジ日時 2023-09-30 23:18:35
合計ジャッジ時間 23,168 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 999 ms
27,316 KB
testcase_01 AC 640 ms
22,284 KB
testcase_02 AC 1,239 ms
28,100 KB
testcase_03 AC 602 ms
25,576 KB
testcase_04 AC 975 ms
28,156 KB
testcase_05 AC 1,203 ms
26,152 KB
testcase_06 AC 996 ms
24,804 KB
testcase_07 AC 904 ms
26,080 KB
testcase_08 AC 1,308 ms
29,992 KB
testcase_09 AC 1,126 ms
25,700 KB
testcase_10 AC 1,071 ms
29,404 KB
testcase_11 AC 977 ms
27,020 KB
testcase_12 AC 811 ms
26,016 KB
testcase_13 AC 863 ms
27,388 KB
testcase_14 AC 690 ms
22,728 KB
testcase_15 AC 1,034 ms
28,212 KB
testcase_16 AC 1,106 ms
28,748 KB
testcase_17 AC 774 ms
23,608 KB
testcase_18 AC 964 ms
26,740 KB
testcase_19 AC 1,403 ms
32,652 KB
testcase_20 AC 16 ms
8,032 KB
testcase_21 AC 16 ms
7,968 KB
testcase_22 AC 16 ms
8,060 KB
testcase_23 AC 14 ms
7,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n,q=map(int,input().split())
ab=[]
for _ in range(q):
    a,b=map(int,input().split())
    a-=1;b-=1
    ab.append((a,b))
def check(x):
    deg=[0]*n
    g=[[] for _ in range(n)]
    q=[]
    for a,b in ab[:x]:
        deg[b]+=1
        g[a].append(b)
    for i in range(n):
        if deg[i]==0:
            q.append(i)
    ans=[]
    while q:
        now=q.pop()
        ans.append(now)
        for to in g[now]:
            deg[to]-=1
            if deg[to]==0:
                q.append(to)
    return len(ans)!=n
r=q+1;l=0
while r-l>1:
    mid=(r+l)//2
    if check(mid):
        r=mid
    else:
        l=mid
if r>q:
    print(-1)
else:
    print(r)
0