結果

問題 No.1660 Matrix Exponentiation
ユーザー shinichi
提出日時 2021-08-28 01:29:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 450 ms / 2,000 ms
コード長 630 bytes
コンパイル時間 225 ms
コンパイル使用メモリ 82,312 KB
実行使用メモリ 198,960 KB
最終ジャッジ日時 2024-11-21 08:37:00
合計ジャッジ時間 5,004 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
import sys


N, K = map(int, input().split())
graph = defaultdict(list)
for _ in range(K):
    r, c = map(lambda x:int(x)-1, input().split())
    graph[r].append(c)

memo = [0] * (N+1)
seen = [False] * (N+1)
sys.setrecursionlimit(1000000)
def dfs(v):
    if memo[v]:
        return memo[v]
    seen[v] = True
    res = 1
    for next_v in graph[v]:
        if seen[next_v] and not memo[next_v]:
            print(-1)
            sys.exit()
        res = max(res, dfs(next_v)+1)
    memo[v] = res
    return res

for i in range(N):
    if memo[i]:
        continue
    dfs(i)
print(max(memo))


0