結果

問題 No.1865 Make Cycle
ユーザー roarisroaris
提出日時 2022-03-04 22:33:53
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 1,295 ms / 3,000 ms
コード長 1,019 bytes
コンパイル時間 1,710 ms
コンパイル使用メモリ 86,552 KB
実行使用メモリ 123,036 KB
最終ジャッジ日時 2023-09-26 01:42:04
合計ジャッジ時間 21,791 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 892 ms
110,492 KB
testcase_01 AC 686 ms
101,156 KB
testcase_02 AC 695 ms
104,336 KB
testcase_03 AC 470 ms
102,300 KB
testcase_04 AC 713 ms
111,172 KB
testcase_05 AC 817 ms
112,504 KB
testcase_06 AC 981 ms
110,348 KB
testcase_07 AC 902 ms
107,908 KB
testcase_08 AC 888 ms
110,372 KB
testcase_09 AC 826 ms
111,916 KB
testcase_10 AC 1,117 ms
115,668 KB
testcase_11 AC 1,008 ms
115,632 KB
testcase_12 AC 784 ms
104,896 KB
testcase_13 AC 892 ms
103,956 KB
testcase_14 AC 741 ms
104,544 KB
testcase_15 AC 1,171 ms
117,932 KB
testcase_16 AC 1,114 ms
114,508 KB
testcase_17 AC 917 ms
108,280 KB
testcase_18 AC 796 ms
113,320 KB
testcase_19 AC 1,295 ms
123,036 KB
testcase_20 AC 87 ms
71,252 KB
testcase_21 AC 88 ms
71,676 KB
testcase_22 AC 89 ms
71,680 KB
testcase_23 AC 88 ms
71,436 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**5+10)
from collections import *

def dfs(v, pv, t):
    global pos
    
    seen[v] = True
    hist.append(v)
    
    for i, nv in G[v]:
        if t<i:
            continue
        
        if finished[nv]:
            continue
        
        if seen[nv] and not finished[nv]:
            pos = nv
            return
        
        dfs(nv, v, t)
        
        if pos!=-1:
            return
    
    hist.pop()
    finished[v] = True

N, Q = map(int, input().split())
G = [[] for _ in range(N)]

for i in range(Q):
    A, B = map(int, input().split())
    G[A-1].append((i+1, B-1))

l, r = 0, Q

while l<=r:
    m = (l+r)//2
    seen = [False]*N
    finished = [False]*N
    pos = -1
    hist = []
    
    for v in range(N):
        if not seen[v]:
            dfs(v, -1, m)
            
            if len(hist)>0:
                r = m-1
                break
    else:
        l = m+1

ans = l

if ans==Q+1:
    print(-1)
else:
    print(ans)
0