結果

問題 No.1865 Make Cycle
ユーザー H3PO4H3PO4
提出日時 2022-03-04 22:26:07
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 982 bytes
コンパイル時間 286 ms
コンパイル使用メモリ 86,980 KB
実行使用メモリ 110,556 KB
最終ジャッジ日時 2023-09-26 01:33:37
合計ジャッジ時間 7,392 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 217 ms
99,008 KB
testcase_02 WA -
testcase_03 AC 247 ms
103,200 KB
testcase_04 AC 255 ms
105,208 KB
testcase_05 AC 235 ms
105,616 KB
testcase_06 AC 237 ms
103,592 KB
testcase_07 AC 258 ms
105,732 KB
testcase_08 AC 286 ms
110,556 KB
testcase_09 AC 233 ms
101,252 KB
testcase_10 AC 281 ms
108,008 KB
testcase_11 AC 259 ms
105,028 KB
testcase_12 AC 250 ms
103,352 KB
testcase_13 WA -
testcase_14 AC 215 ms
98,212 KB
testcase_15 AC 276 ms
105,540 KB
testcase_16 AC 270 ms
109,824 KB
testcase_17 AC 234 ms
101,512 KB
testcase_18 AC 239 ms
103,240 KB
testcase_19 AC 296 ms
110,100 KB
testcase_20 AC 93 ms
71,580 KB
testcase_21 AC 91 ms
71,628 KB
testcase_22 AC 91 ms
71,580 KB
testcase_23 AC 92 ms
71,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

input = sys.stdin.buffer.readline

N, Q = map(int, input().split())
edges = tuple(tuple(int(x) - 1 for x in input().split()) for _ in range(Q))
deg = [0] * N
G = [[] for _ in range(N)]
for i, (a, b) in enumerate(edges):
    deg[b] += 1
    G[a].append((i, b))

deg_is_zero = set(i for i, x in enumerate(deg) if not x)
d = deque(deg_is_zero)
nonvisited_edge = [True] * Q


def dfs(d):
    while d:
        v = d.popleft()
        for i, x in G[v]:
            nonvisited_edge[i] = False
            deg[x] -= 1
            if not deg[x]:
                d.append(x)
                deg_is_zero.add(x)


dfs(d)

if len(deg_is_zero) == N:
    print(-1)

for i in range(Q, 0, -1):
    if nonvisited_edge[i - 1]:
        b = edges[i - 1][1]
        deg[b] -= 1
        nonvisited_edge[i - 1] = False
        if not deg[b]:
            deg_is_zero.add(b)
            dfs(deque([b]))
            if len(deg_is_zero) == N:
                print(i)
0