結果

問題 No.1865 Make Cycle
ユーザー ikoma
提出日時 2022-03-04 23:03:37
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 901 bytes
コンパイル時間 162 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 318,220 KB
最終ジャッジ日時 2024-07-18 21:39:43
合計ジャッジ時間 17,414 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3 WA * 1
other AC * 19 WA * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

from collections import deque
N,Q=map(int,input().split())
AB=[list(map(lambda x:int(x)-1,input().split())) for _ in range(Q)]

from collections import defaultdict,deque
def check_cycle(n):
    ins = defaultdict(int)
    outs = defaultdict(list)
    for a,b in AB[:n]:
        ins[b]+=1
        outs[a].append(b)
    res = []
    que = deque([i for i in range(N) if ins[i] == 0])
    while que:
        v = que.popleft()
        res.append(v)
        for v2 in outs[v]:
            ins[v2] -= 1
            if ins[v2] == 0:
                que.append(v2)
    if len(res) != N:
        return True # 閉路検出
    return False

def solve():
    if not check_cycle(N):
        print(-1)
        return
    ng,ok = 0,Q
    while ng+1<ok:
        m = (ng+ok)//2
        if check_cycle(m):
            ok = m
        else:
            ng = m
    print(ok)

solve()
0