結果

問題 No.1660 Matrix Exponentiation
コンテスト
ユーザー rin204
提出日時 2021-08-27 21:48:11
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 284 ms / 2,000 ms
コード長 698 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 905 ms
コンパイル使用メモリ 84,864 KB
実行使用メモリ 178,432 KB
最終ジャッジ日時 2026-05-27 12:00:55
合計ジャッジ時間 4,820 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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