結果

問題 No.1865 Make Cycle
ユーザー H3PO4H3PO4
提出日時 2022-03-04 22:29:17
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,005 bytes
コンパイル時間 272 ms
コンパイル使用メモリ 87,228 KB
実行使用メモリ 110,408 KB
最終ジャッジ日時 2023-09-26 01:36:16
合計ジャッジ時間 7,170 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 214 ms
98,856 KB
testcase_02 WA -
testcase_03 AC 244 ms
103,176 KB
testcase_04 AC 246 ms
105,024 KB
testcase_05 AC 231 ms
105,716 KB
testcase_06 AC 227 ms
103,632 KB
testcase_07 AC 249 ms
105,588 KB
testcase_08 AC 283 ms
110,408 KB
testcase_09 AC 228 ms
101,164 KB
testcase_10 AC 277 ms
107,872 KB
testcase_11 AC 252 ms
105,084 KB
testcase_12 AC 245 ms
103,500 KB
testcase_13 WA -
testcase_14 AC 207 ms
98,048 KB
testcase_15 AC 272 ms
105,760 KB
testcase_16 AC 251 ms
109,760 KB
testcase_17 AC 220 ms
101,652 KB
testcase_18 AC 226 ms
103,608 KB
testcase_19 AC 289 ms
110,096 KB
testcase_20 AC 87 ms
71,688 KB
testcase_21 AC 88 ms
71,700 KB
testcase_22 AC 87 ms
71,608 KB
testcase_23 AC 89 ms
71,608 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)
                exit()
0