結果

問題 No.1865 Make Cycle
ユーザー ああいいああいい
提出日時 2022-03-04 22:29:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 336 ms / 3,000 ms
コード長 1,031 bytes
コンパイル時間 639 ms
コンパイル使用メモリ 87,200 KB
実行使用メモリ 115,816 KB
最終ジャッジ日時 2023-09-26 01:36:27
合計ジャッジ時間 8,256 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 331 ms
109,740 KB
testcase_01 AC 243 ms
99,224 KB
testcase_02 AC 328 ms
108,964 KB
testcase_03 AC 280 ms
103,424 KB
testcase_04 AC 266 ms
105,892 KB
testcase_05 AC 237 ms
101,588 KB
testcase_06 AC 240 ms
102,228 KB
testcase_07 AC 296 ms
104,284 KB
testcase_08 AC 336 ms
112,308 KB
testcase_09 AC 234 ms
100,752 KB
testcase_10 AC 315 ms
111,396 KB
testcase_11 AC 264 ms
105,120 KB
testcase_12 AC 266 ms
103,104 KB
testcase_13 AC 336 ms
105,284 KB
testcase_14 AC 230 ms
98,688 KB
testcase_15 AC 310 ms
108,296 KB
testcase_16 AC 288 ms
108,692 KB
testcase_17 AC 239 ms
101,072 KB
testcase_18 AC 235 ms
101,672 KB
testcase_19 AC 331 ms
115,816 KB
testcase_20 AC 73 ms
71,264 KB
testcase_21 AC 73 ms
71,048 KB
testcase_22 AC 72 ms
71,324 KB
testcase_23 AC 70 ms
71,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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