結果

問題 No.1660 Matrix Exponentiation
ユーザー shinichishinichi
提出日時 2021-08-28 01:29:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 468 ms / 2,000 ms
コード長 630 bytes
コンパイル時間 2,631 ms
コンパイル使用メモリ 82,100 KB
実行使用メモリ 198,460 KB
最終ジャッジ日時 2024-05-01 06:30:32
合計ジャッジ時間 6,049 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
54,252 KB
testcase_01 AC 38 ms
54,020 KB
testcase_02 AC 38 ms
55,120 KB
testcase_03 AC 40 ms
54,572 KB
testcase_04 AC 39 ms
54,020 KB
testcase_05 AC 39 ms
55,800 KB
testcase_06 AC 39 ms
54,060 KB
testcase_07 AC 39 ms
53,800 KB
testcase_08 AC 40 ms
54,692 KB
testcase_09 AC 63 ms
80,900 KB
testcase_10 AC 63 ms
81,240 KB
testcase_11 AC 42 ms
55,540 KB
testcase_12 AC 40 ms
55,700 KB
testcase_13 AC 39 ms
53,612 KB
testcase_14 AC 40 ms
55,068 KB
testcase_15 AC 48 ms
56,988 KB
testcase_16 AC 41 ms
54,372 KB
testcase_17 AC 42 ms
55,964 KB
testcase_18 AC 53 ms
58,684 KB
testcase_19 AC 257 ms
90,160 KB
testcase_20 AC 198 ms
89,692 KB
testcase_21 AC 221 ms
82,936 KB
testcase_22 AC 81 ms
83,972 KB
testcase_23 AC 159 ms
86,900 KB
testcase_24 AC 460 ms
198,460 KB
testcase_25 AC 166 ms
95,052 KB
testcase_26 AC 468 ms
197,880 KB
testcase_27 AC 153 ms
93,584 KB
testcase_28 AC 220 ms
90,720 KB
testcase_29 AC 375 ms
181,756 KB
権限があれば一括ダウンロードができます

ソースコード

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