結果

問題 No.1865 Make Cycle
ユーザー ああいいああいい
提出日時 2022-03-04 22:22:23
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 921 bytes
コンパイル時間 252 ms
コンパイル使用メモリ 87,224 KB
実行使用メモリ 93,452 KB
最終ジャッジ日時 2023-09-26 01:29:21
合計ジャッジ時間 6,446 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 199 ms
90,312 KB
testcase_05 AC 181 ms
90,372 KB
testcase_06 AC 181 ms
88,432 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 178 ms
89,092 KB
testcase_10 WA -
testcase_11 AC 204 ms
89,012 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 211 ms
90,048 KB
testcase_17 AC 177 ms
87,332 KB
testcase_18 AC 178 ms
89,824 KB
testcase_19 AC 240 ms
93,452 KB
testcase_20 AC 69 ms
71,508 KB
testcase_21 AC 68 ms
71,512 KB
testcase_22 AC 68 ms
71,288 KB
testcase_23 AC 67 ms
71,244 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N,Q = map(int,input().split())
G = [[] for _ in range(N+1)]
Gnum = [0] * (N + 1)
edge = []
for _ in range(Q):
    a,b = map(int,input().split())
    G[a].append(b)
    Gnum[b] += 1
    edge.append((a,b))

count = 0
top = []
stack = []
for i in range(1,N+1):
    if Gnum[i] == 0:
        stack.append(i)
        count += 1
while stack:
    now = stack.pop()
    for v in G[now]:
        Gnum[v] -= 1
        if Gnum[v] == 0:
            count += 1
            stack.append(v)
import sys
if count == N:
    print(-1)
    exit()
for i,u in enumerate(reversed(edge)):
    a,b = u
    Gnum[b] -= 1
    if Gnum[b] == 0:
        count += 1
        stack.append(b)
        while stack:
            now = stack.pop()
            for v in G[now]:
                Gnum[v] -= 1
                if Gnum[v] == 0:
                    count += 1
                    stack.append(v)
    if count == N:
        print(Q - i)
        exit()
0