結果

問題 No.1865 Make Cycle
ユーザー mkawa2mkawa2
提出日時 2022-03-04 23:21:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 256 ms / 3,000 ms
コード長 1,458 bytes
コンパイル時間 401 ms
コンパイル使用メモリ 86,948 KB
実行使用メモリ 96,472 KB
最終ジャッジ日時 2023-09-26 02:38:11
合計ジャッジ時間 6,859 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 243 ms
91,100 KB
testcase_01 AC 185 ms
87,780 KB
testcase_02 AC 249 ms
91,692 KB
testcase_03 AC 211 ms
89,844 KB
testcase_04 AC 215 ms
92,560 KB
testcase_05 AC 194 ms
91,616 KB
testcase_06 AC 207 ms
90,456 KB
testcase_07 AC 222 ms
90,888 KB
testcase_08 AC 252 ms
93,408 KB
testcase_09 AC 189 ms
90,984 KB
testcase_10 AC 235 ms
92,752 KB
testcase_11 AC 207 ms
91,460 KB
testcase_12 AC 196 ms
89,816 KB
testcase_13 AC 256 ms
91,192 KB
testcase_14 AC 177 ms
87,772 KB
testcase_15 AC 221 ms
92,316 KB
testcase_16 AC 221 ms
93,228 KB
testcase_17 AC 191 ms
89,612 KB
testcase_18 AC 177 ms
91,576 KB
testcase_19 AC 254 ms
96,472 KB
testcase_20 AC 72 ms
71,320 KB
testcase_21 AC 71 ms
71,368 KB
testcase_22 AC 72 ms
71,400 KB
testcase_23 AC 74 ms
71,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()
dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = (1 << 63)-1
# inf = (1 << 31)-1
md = 10**9+7
# md = 998244353

def topo(st):
    global cnt
    while st:
        u = st.pop()
        for v, i in to[u]:
            if fin[i]: continue
            deg[v] -= 1
            fin[i] = 1
            cnt += 1
            if deg[v] == 0:
                st.append(v)

n, q = LI()

to = [[] for _ in range(n)]
ee = []
deg = [0]*n

for i in range(q):
    u, v = LI1()
    to[u].append((v, i))
    deg[v] += 1
    ee.append((u, v))

cnt = 0
st = [u for u in range(n) if deg[u] == 0]
fin = [0]*q
topo(st)

if cnt == q:
    print(-1)
    exit()

for i in range(q)[::-1]:
    if fin[i]: continue
    fin[i] = 1
    u, v = ee[i]
    deg[v] -= 1
    cnt += 1
    if deg[v] == 0: topo([v])
    if cnt == q:
        print(i+1)
        break
    # print(cnt, fin)
0