結果

問題 No.1660 Matrix Exponentiation
ユーザー hir355hir355
提出日時 2021-08-27 22:47:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 272 ms / 2,000 ms
コード長 653 bytes
コンパイル時間 214 ms
コンパイル使用メモリ 82,552 KB
実行使用メモリ 101,004 KB
最終ジャッジ日時 2024-05-01 03:32:55
合計ジャッジ時間 4,002 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
52,128 KB
testcase_01 AC 34 ms
52,344 KB
testcase_02 AC 36 ms
52,404 KB
testcase_03 AC 35 ms
52,616 KB
testcase_04 AC 34 ms
52,792 KB
testcase_05 AC 33 ms
53,312 KB
testcase_06 AC 36 ms
52,444 KB
testcase_07 AC 35 ms
53,804 KB
testcase_08 AC 34 ms
52,376 KB
testcase_09 AC 53 ms
76,212 KB
testcase_10 AC 53 ms
77,072 KB
testcase_11 AC 52 ms
76,948 KB
testcase_12 AC 34 ms
52,072 KB
testcase_13 AC 34 ms
52,124 KB
testcase_14 AC 34 ms
53,972 KB
testcase_15 AC 43 ms
56,008 KB
testcase_16 AC 35 ms
53,876 KB
testcase_17 AC 38 ms
54,196 KB
testcase_18 AC 47 ms
56,472 KB
testcase_19 AC 213 ms
89,892 KB
testcase_20 AC 182 ms
89,756 KB
testcase_21 AC 186 ms
84,204 KB
testcase_22 AC 87 ms
85,040 KB
testcase_23 AC 144 ms
85,756 KB
testcase_24 AC 237 ms
98,060 KB
testcase_25 AC 205 ms
101,004 KB
testcase_26 AC 239 ms
97,988 KB
testcase_27 AC 151 ms
97,716 KB
testcase_28 AC 272 ms
95,912 KB
testcase_29 AC 201 ms
97,596 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 = 0
            for node2 in g2[node]:
                if t < d[node2]:
                    t = d[node2]
            d[node] = t + 1
            s.append(node)
if -1 in d:
    print(-1)
else:
    print(max(d) + 1)
0