結果

問題 No.1660 Matrix Exponentiation
ユーザー hir355hir355
提出日時 2021-08-27 22:41:24
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 661 bytes
コンパイル時間 195 ms
コンパイル使用メモリ 82,524 KB
実行使用メモリ 101,136 KB
最終ジャッジ日時 2024-05-01 03:26:28
合計ジャッジ時間 4,075 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,416 KB
testcase_01 AC 37 ms
52,560 KB
testcase_02 AC 37 ms
52,604 KB
testcase_03 AC 37 ms
53,940 KB
testcase_04 AC 37 ms
52,076 KB
testcase_05 AC 37 ms
53,148 KB
testcase_06 AC 39 ms
52,832 KB
testcase_07 AC 35 ms
53,528 KB
testcase_08 AC 36 ms
53,260 KB
testcase_09 AC 56 ms
76,744 KB
testcase_10 AC 54 ms
76,580 KB
testcase_11 AC 52 ms
76,636 KB
testcase_12 AC 36 ms
53,340 KB
testcase_13 AC 37 ms
53,140 KB
testcase_14 AC 37 ms
53,784 KB
testcase_15 WA -
testcase_16 AC 37 ms
52,656 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 82 ms
85,504 KB
testcase_23 WA -
testcase_24 AC 239 ms
98,004 KB
testcase_25 AC 204 ms
101,136 KB
testcase_26 AC 249 ms
98,260 KB
testcase_27 AC 148 ms
97,884 KB
testcase_28 AC 262 ms
95,756 KB
testcase_29 AC 205 ms
97,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n, m = map(int, input().split())
g = [[] for _ in range(n)]
g2 = [[] for _ in range(n)]
inv = [0] * n
for i in range(m):
    a, b = map(int, input().split())
    g[a - 1].append(b - 1)
    g2[b - 1].append(a - 1)
    inv[b - 1] += 1
s = []
d = [-1] * n
for i in range(n):
    if inv[i] == 0:
        s.append(i)
        d[i] = 0
while s:
    p = s.pop()
    for node in g[p]:
        inv[node] -= 1
        if inv[node] == 0:
            t = -1
            for node2 in g2[node]:
                if t < d[node2]:
                    t = d[node2]
            d[node] = d[node2] + 1
            s.append(node)
if -1 in d:
    print(-1)
else:
    print(max(d) + 1)
0