結果

問題 No.1660 Matrix Exponentiation
ユーザー とりゐとりゐ
提出日時 2022-03-30 11:58:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 734 ms / 2,000 ms
コード長 1,365 bytes
コンパイル時間 163 ms
コンパイル使用メモリ 82,172 KB
実行使用メモリ 247,396 KB
最終ジャッジ日時 2024-04-26 20:13:17
合計ジャッジ時間 7,318 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,280 KB
testcase_01 AC 39 ms
53,036 KB
testcase_02 AC 38 ms
52,588 KB
testcase_03 AC 39 ms
52,296 KB
testcase_04 AC 39 ms
52,688 KB
testcase_05 AC 38 ms
52,404 KB
testcase_06 AC 37 ms
52,880 KB
testcase_07 AC 38 ms
52,752 KB
testcase_08 AC 39 ms
52,076 KB
testcase_09 AC 100 ms
100,768 KB
testcase_10 AC 101 ms
100,584 KB
testcase_11 AC 48 ms
64,380 KB
testcase_12 AC 40 ms
52,404 KB
testcase_13 AC 38 ms
53,776 KB
testcase_14 AC 40 ms
52,892 KB
testcase_15 AC 48 ms
56,980 KB
testcase_16 AC 40 ms
53,476 KB
testcase_17 AC 40 ms
53,632 KB
testcase_18 AC 52 ms
58,056 KB
testcase_19 AC 418 ms
113,520 KB
testcase_20 AC 337 ms
110,688 KB
testcase_21 AC 316 ms
94,580 KB
testcase_22 AC 103 ms
86,100 KB
testcase_23 AC 258 ms
98,988 KB
testcase_24 AC 711 ms
230,468 KB
testcase_25 AC 325 ms
120,512 KB
testcase_26 AC 709 ms
230,080 KB
testcase_27 AC 46 ms
64,784 KB
testcase_28 AC 438 ms
101,236 KB
testcase_29 AC 734 ms
247,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**6)

def scc(N, G, RG):
    order = []
    used = [0]*N
    group = [None]*N
    def dfs(s):
        used[s] = 1
        for t in G[s]:
            if not used[t]:
                dfs(t)
        order.append(s)
    def rdfs(s, col):
        group[s] = col
        used[s] = 1
        for t in RG[s]:
            if not used[t]:
                rdfs(t, col)
    for i in range(N):
        if not used[i]:
            dfs(i)
    used = [0]*N
    label = 0
    for s in reversed(order):
        if not used[s]:
            rdfs(s, label)
            label += 1
    return label, group

def construct(N, G, label, group):
    G0 = [set() for i in range(label)]
    GP = [[] for i in range(label)]
    for v in range(N):
        lbs = group[v]
        for w in G[v]:
            lbt = group[w]
            if lbs == lbt:
                continue
            G0[lbs].add(lbt)
        GP[lbs].append(v)
    return G0, GP

n,m=map(int,input().split())
edge=[[] for i in range(n)]
redge=[[] for i in range(n)]
for _ in range(m):
  a,b=map(lambda x:int(x)-1,input().split())
  if a==b:
    print(-1)
    exit()
  edge[a].append(b)
  redge[b].append(a)

label,group=scc(n,edge,redge)
if label!=n:
  print(-1)
  exit()

dp=[1]*n
G0,GP=construct(n,edge,n,group)
for i in range(n):
  for j in G0[i]:
    dp[j]=max(dp[j],dp[i]+1)
print(max(dp))
0