結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,756 KB
testcase_01 AC 38 ms
52,800 KB
testcase_02 AC 40 ms
51,936 KB
testcase_03 AC 39 ms
51,948 KB
testcase_04 AC 39 ms
52,368 KB
testcase_05 AC 39 ms
52,460 KB
testcase_06 AC 39 ms
51,932 KB
testcase_07 AC 38 ms
52,736 KB
testcase_08 AC 38 ms
52,744 KB
testcase_09 AC 65 ms
81,288 KB
testcase_10 AC 66 ms
82,444 KB
testcase_11 AC 60 ms
78,992 KB
testcase_12 AC 38 ms
52,756 KB
testcase_13 AC 38 ms
52,360 KB
testcase_14 AC 40 ms
52,360 KB
testcase_15 AC 48 ms
55,672 KB
testcase_16 AC 40 ms
53,484 KB
testcase_17 AC 40 ms
53,180 KB
testcase_18 AC 52 ms
61,772 KB
testcase_19 AC 262 ms
89,212 KB
testcase_20 AC 212 ms
89,948 KB
testcase_21 AC 210 ms
81,276 KB
testcase_22 AC 93 ms
87,256 KB
testcase_23 AC 172 ms
85,772 KB
testcase_24 RE -
testcase_25 AC 179 ms
97,772 KB
testcase_26 RE -
testcase_27 AC 127 ms
85,676 KB
testcase_28 AC 204 ms
92,044 KB
testcase_29 AC 156 ms
85,744 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