結果

問題 No.1660 Matrix Exponentiation
ユーザー googol_S0googol_S0
提出日時 2021-08-27 23:12:24
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,005 bytes
コンパイル時間 135 ms
コンパイル使用メモリ 82,404 KB
実行使用メモリ 112,700 KB
最終ジャッジ日時 2024-05-01 04:04:36
合計ジャッジ時間 4,420 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
53,232 KB
testcase_01 AC 33 ms
52,992 KB
testcase_02 AC 36 ms
53,344 KB
testcase_03 AC 35 ms
53,936 KB
testcase_04 AC 34 ms
54,232 KB
testcase_05 AC 32 ms
53,316 KB
testcase_06 AC 33 ms
52,876 KB
testcase_07 AC 33 ms
52,784 KB
testcase_08 AC 32 ms
53,568 KB
testcase_09 AC 90 ms
97,604 KB
testcase_10 AC 86 ms
97,800 KB
testcase_11 AC 39 ms
61,756 KB
testcase_12 AC 32 ms
53,372 KB
testcase_13 AC 33 ms
53,556 KB
testcase_14 AC 36 ms
52,748 KB
testcase_15 AC 45 ms
62,008 KB
testcase_16 AC 35 ms
54,856 KB
testcase_17 AC 36 ms
53,792 KB
testcase_18 AC 49 ms
64,784 KB
testcase_19 AC 372 ms
103,624 KB
testcase_20 AC 292 ms
101,656 KB
testcase_21 AC 279 ms
89,132 KB
testcase_22 AC 105 ms
87,752 KB
testcase_23 AC 245 ms
95,496 KB
testcase_24 RE -
testcase_25 AC 208 ms
110,328 KB
testcase_26 RE -
testcase_27 AC 38 ms
60,648 KB
testcase_28 AC 377 ms
112,700 KB
testcase_29 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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