結果

問題 No.1660 Matrix Exponentiation
ユーザー NatsubiSogan
提出日時 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
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25 RE * 2
権限があれば一括ダウンロードができます

ソースコード

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