結果

問題 No.1865 Make Cycle
ユーザー H3PO4H3PO4
提出日時 2022-03-04 22:20:04
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 792 bytes
コンパイル時間 303 ms
コンパイル使用メモリ 87,036 KB
実行使用メモリ 112,940 KB
最終ジャッジ日時 2023-09-26 01:23:50
合計ジャッジ時間 7,874 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 258 ms
103,920 KB
testcase_05 AC 239 ms
103,228 KB
testcase_06 AC 240 ms
101,540 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 234 ms
101,204 KB
testcase_10 WA -
testcase_11 AC 260 ms
101,764 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 254 ms
107,356 KB
testcase_17 AC 228 ms
99,240 KB
testcase_18 AC 233 ms
101,356 KB
testcase_19 AC 294 ms
112,940 KB
testcase_20 AC 94 ms
71,752 KB
testcase_21 AC 95 ms
71,668 KB
testcase_22 AC 93 ms
71,304 KB
testcase_23 AC 94 ms
71,776 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 a, b in edges:
    deg[b] += 1
    G[a].append(b)

deg_is_zero = set(i for i, x in enumerate(deg) if not x)
d = deque(deg_is_zero)


def dfs(d):
    while d:
        v = d.popleft()
        for x in G[v]:
            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):
    b = edges[i - 1][1]
    deg[b] -= 1
    if not deg[b]:
        deg_is_zero.add(b)
        dfs(deque([b]))
        if len(deg_is_zero) == N:
            print(i)
0