結果
問題 | No.1660 Matrix Exponentiation |
ユーザー |
![]() |
提出日時 | 2021-08-27 23:12:45 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 883 ms / 2,000 ms |
コード長 | 1,045 bytes |
コンパイル時間 | 156 ms |
コンパイル使用メモリ | 82,308 KB |
実行使用メモリ | 288,016 KB |
最終ジャッジ日時 | 2024-11-21 05:05:45 |
合計ジャッジ時間 | 6,742 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 |
ソースコード
import sys sys.setrecursionlimit(10**8) N,M=map(int,input().split()) G=[[] for i in range(N)] for i in range(M): a,b=map(int,input().split()) a-=1 b-=1 if a==b: print(-1) exit() G[a].append(b) def SCC(G): D=[] C=[len(G)-1] U=[1]*len(G) def DFS(x): if U[x]: U[x]=0 for i in range(len(G[x])): DFS(G[x][i]) D.append(x) for i in range(len(G)): if U[i]: DFS(i) GR=[[] for i in range(len(G))] for i in range(len(G)): for j in range(len(G[i])): GR[G[i][j]].append(i) R=[] U=[1]*len(G) def DFSR(x): if U[x]: R[-1].append(x) U[x]=0 for i in range(len(GR[x])): DFSR(GR[x][i]) for i in range(len(G)-1,-1,-1): if U[D[i]]: R.append([]) DFSR(D[i]) return R X=SCC(G) if max([len(X[i]) for i in range(len(X))])>1: print(-1) exit() DP=[-1]*N def f(x): if DP[x]!=-1: return DP[x] DP[x]=0 for i in range(len(G[x])): DP[x]=max(DP[x],f(G[x][i])+1) return DP[x] print(max([f(i)for i in range(N)])+1)