結果
| 問題 | No.1660 Matrix Exponentiation | 
| コンテスト | |
| ユーザー |  vwxyz | 
| 提出日時 | 2024-08-15 10:34:47 | 
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 669 ms / 2,000 ms | 
| コード長 | 510 bytes | 
| コンパイル時間 | 229 ms | 
| コンパイル使用メモリ | 12,416 KB | 
| 実行使用メモリ | 28,672 KB | 
| 最終ジャッジ日時 | 2024-08-15 10:34:55 | 
| 合計ジャッジ時間 | 7,371 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 27 | 
ソースコード
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)
            
            
            
        