結果

問題 No.1660 Matrix Exponentiation
ユーザー NatsubiSoganNatsubiSogan
提出日時 2021-08-28 12:56:46
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 791 bytes
コンパイル時間 232 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 99,584 KB
最終ジャッジ日時 2024-05-01 15:50:12
合計ジャッジ時間 4,786 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
52,352 KB
testcase_01 AC 42 ms
51,840 KB
testcase_02 AC 42 ms
52,096 KB
testcase_03 AC 43 ms
52,224 KB
testcase_04 AC 43 ms
52,096 KB
testcase_05 AC 42 ms
52,096 KB
testcase_06 AC 43 ms
51,840 KB
testcase_07 AC 42 ms
52,096 KB
testcase_08 AC 43 ms
52,096 KB
testcase_09 AC 73 ms
80,768 KB
testcase_10 AC 74 ms
80,896 KB
testcase_11 AC 66 ms
77,952 KB
testcase_12 AC 43 ms
51,840 KB
testcase_13 AC 43 ms
52,480 KB
testcase_14 AC 44 ms
52,224 KB
testcase_15 AC 53 ms
55,168 KB
testcase_16 AC 44 ms
52,992 KB
testcase_17 AC 45 ms
52,736 KB
testcase_18 AC 60 ms
61,440 KB
testcase_19 AC 294 ms
89,344 KB
testcase_20 AC 242 ms
90,240 KB
testcase_21 AC 237 ms
81,664 KB
testcase_22 AC 114 ms
87,260 KB
testcase_23 AC 202 ms
86,016 KB
testcase_24 RE -
testcase_25 AC 200 ms
98,036 KB
testcase_26 RE -
testcase_27 AC 151 ms
85,892 KB
testcase_28 AC 235 ms
92,288 KB
testcase_29 AC 183 ms
86,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n, m = map(int, input().split())
G = [[] for i in range(n)]
d = [0] * n
for _ in range(m):
    u, v = map(int, input().split())
    G[u - 1].append(v - 1)
    d[v - 1] += 1
def topological_sort(G):
    s = []
    for i in range(n):
        if d[i] == 0: s.append(i)
    ans = []
    while s:
        u = s.pop()
        ans.append(u)
        for v in G[u]:
            d[v] -= 1
            if d[v] == 0: s.append(v)
    if len(ans) != n: return -1
    return ans
if topological_sort(G) == -1: exit(print(-1)) 
visit = [False] * n
dp = [0] * n
def dfs(now):
    if visit[now]:
        return dp[now]
    visit[now] = True
    res = 0
    for i in G[now]:
        res = max(res, dfs(i) + 1)
    dp[now] = res
    return res
ans = 0
for i in range(n):
    ans = max(ans, dfs(i))
print(ans + 1)
0