結果

問題 No.1660 Matrix Exponentiation
ユーザー googol_S0googol_S0
提出日時 2021-08-27 23:12:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 904 ms / 2,000 ms
コード長 1,045 bytes
コンパイル時間 186 ms
コンパイル使用メモリ 81,992 KB
実行使用メモリ 289,480 KB
最終ジャッジ日時 2024-05-01 04:06:04
合計ジャッジ時間 7,055 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,304 KB
testcase_01 AC 35 ms
54,188 KB
testcase_02 AC 36 ms
53,552 KB
testcase_03 AC 36 ms
53,424 KB
testcase_04 AC 35 ms
53,612 KB
testcase_05 AC 35 ms
53,172 KB
testcase_06 AC 35 ms
52,684 KB
testcase_07 AC 36 ms
52,996 KB
testcase_08 AC 37 ms
53,536 KB
testcase_09 AC 97 ms
97,732 KB
testcase_10 AC 94 ms
97,832 KB
testcase_11 AC 39 ms
61,668 KB
testcase_12 AC 36 ms
53,164 KB
testcase_13 AC 37 ms
53,604 KB
testcase_14 AC 37 ms
53,512 KB
testcase_15 AC 48 ms
61,740 KB
testcase_16 AC 39 ms
54,516 KB
testcase_17 AC 38 ms
54,916 KB
testcase_18 AC 54 ms
66,356 KB
testcase_19 AC 431 ms
103,888 KB
testcase_20 AC 341 ms
102,232 KB
testcase_21 AC 311 ms
89,384 KB
testcase_22 AC 114 ms
87,832 KB
testcase_23 AC 292 ms
95,672 KB
testcase_24 AC 904 ms
269,956 KB
testcase_25 AC 256 ms
111,100 KB
testcase_26 AC 873 ms
269,988 KB
testcase_27 AC 41 ms
60,824 KB
testcase_28 AC 439 ms
113,048 KB
testcase_29 AC 894 ms
289,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
0