結果

問題 No.1865 Make Cycle
ユーザー chineristACchineristAC
提出日時 2022-03-04 23:02:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 675 ms / 3,000 ms
コード長 927 bytes
コンパイル時間 417 ms
コンパイル使用メモリ 87,092 KB
実行使用メモリ 164,992 KB
最終ジャッジ日時 2023-09-26 02:15:09
合計ジャッジ時間 11,763 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 462 ms
123,700 KB
testcase_01 AC 322 ms
107,908 KB
testcase_02 AC 474 ms
128,416 KB
testcase_03 AC 285 ms
118,880 KB
testcase_04 AC 411 ms
137,160 KB
testcase_05 AC 479 ms
139,208 KB
testcase_06 AC 444 ms
121,688 KB
testcase_07 AC 420 ms
123,344 KB
testcase_08 AC 522 ms
137,160 KB
testcase_09 AC 450 ms
133,332 KB
testcase_10 AC 506 ms
142,504 KB
testcase_11 AC 469 ms
133,104 KB
testcase_12 AC 398 ms
123,728 KB
testcase_13 AC 424 ms
122,712 KB
testcase_14 AC 362 ms
121,556 KB
testcase_15 AC 521 ms
134,128 KB
testcase_16 AC 549 ms
143,332 KB
testcase_17 AC 403 ms
114,724 KB
testcase_18 AC 474 ms
138,968 KB
testcase_19 AC 675 ms
164,992 KB
testcase_20 AC 109 ms
74,748 KB
testcase_21 AC 108 ms
74,532 KB
testcase_22 AC 108 ms
74,340 KB
testcase_23 AC 108 ms
74,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys,random,bisect
from collections import deque,defaultdict
from heapq import heapify,heappop,heappush
from itertools import permutations
from math import log,gcd

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

N,M = mi()
E = []
for _ in range(M):
    u,v = mi()
    E.append((u-1,v-1))

def cond(k):
    edge = [[] for v in range(N)]
    deg = [0] * N
    for i in range(k):
        u,v = E[i]
        edge[u].append(v)
        deg[v] += 1
    
    deq = deque([v for v in range(N) if deg[v]==0])
    while deq:
        v = deq.popleft()
        for nv in edge[v]:
            deg[nv] -= 1
            if deg[nv]==0:
                deq.append(nv)
    
    return any(deg[v] for v in range(N))

ok = M+1
ng = 0
while ok-ng>1:
    mid = (ok+ng)//2
    if cond(mid):
        ok = mid
    else:
        ng = mid

if ok==M+1:
    print(-1)
else:
    print(ok)

0