結果

問題 No.1660 Matrix Exponentiation
コンテスト
ユーザー vwxyz
提出日時 2024-08-15 10:34:47
言語 Python3
(3.14.7 + numpy 2.5.2 + scipy 1.18.0 + ACL)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 437 ms / 2,000 ms
+ 301µs
コード長 510 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 304 ms
コンパイル使用メモリ 21,540 KB
実行使用メモリ 33,148 KB
最終ジャッジ日時 2026-08-25 01:30:42
合計ジャッジ時間 8,203 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

N,K=map(int,input().split())
graph=[[] for x in range(N)]
degree=[0]*N
for k in range(K):
    r,c=map(int,input().split())
    r-=1;c-=1
    graph[r].append(c)
    degree[c]+=1
queue=[]
inf=1<<30
dp=[-inf]*N
for x in range(N):
    if degree[x]==0:
        queue.append(x)
        dp[x]=0
while queue:
    x=queue.pop()
    for y in graph[x]:
        dp[y]=max(dp[y],dp[x]+1)
        degree[y]-=1
        if degree[y]==0:
            queue.append(y)
if sum(degree):
    ans=-1
else:
    ans=max(dp)+1
print(ans)
0