結果

問題 No.1660 Matrix Exponentiation
ユーザー 👑 rin204rin204
提出日時 2021-08-27 21:48:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 502 ms / 2,000 ms
コード長 698 bytes
コンパイル時間 318 ms
コンパイル使用メモリ 87,144 KB
実行使用メモリ 187,164 KB
最終ジャッジ日時 2023-08-13 08:35:33
合計ジャッジ時間 6,140 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,232 KB
testcase_01 AC 72 ms
71,028 KB
testcase_02 AC 75 ms
71,372 KB
testcase_03 AC 70 ms
71,024 KB
testcase_04 AC 72 ms
71,372 KB
testcase_05 AC 73 ms
71,500 KB
testcase_06 AC 72 ms
71,324 KB
testcase_07 AC 73 ms
71,332 KB
testcase_08 AC 72 ms
71,376 KB
testcase_09 AC 89 ms
78,440 KB
testcase_10 AC 82 ms
78,372 KB
testcase_11 AC 82 ms
77,448 KB
testcase_12 AC 73 ms
71,232 KB
testcase_13 AC 71 ms
71,272 KB
testcase_14 AC 72 ms
71,404 KB
testcase_15 AC 78 ms
71,340 KB
testcase_16 AC 72 ms
71,376 KB
testcase_17 AC 74 ms
71,060 KB
testcase_18 AC 83 ms
71,224 KB
testcase_19 AC 270 ms
86,788 KB
testcase_20 AC 216 ms
85,256 KB
testcase_21 AC 227 ms
82,956 KB
testcase_22 AC 118 ms
81,256 KB
testcase_23 AC 189 ms
83,436 KB
testcase_24 AC 495 ms
187,164 KB
testcase_25 AC 154 ms
86,076 KB
testcase_26 AC 502 ms
186,600 KB
testcase_27 AC 155 ms
87,796 KB
testcase_28 AC 344 ms
93,544 KB
testcase_29 AC 400 ms
177,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10 ** 9)

n, k = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(k):
    r, c = map(int, input().split())
    r -= 1
    c -= 1
    edges[r].append(c)
    
dist = [-1] * n
used = [False] * n

def dfs(pos):
    dist[pos] = max(dist[pos], 0)
    for npos in edges[pos]:
        if used[npos]:
            print(-1)
            exit()
        if dist[npos] != -1:
            dist[pos] = max(dist[pos], dist[npos] + 1)
            continue
        dfs(npos)
        dist[pos] = max(dist[pos], dist[npos] + 1)

for i in range(n):
    if dist[i] != -1:
        continue
    used[i] = True
    dfs(i)
    used[i] = False
    
print(max(dist) + 1)
0