結果

問題 No.1660 Matrix Exponentiation
ユーザー taiga0629kyoprotaiga0629kyopro
提出日時 2021-08-27 22:17:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 232 ms / 2,000 ms
コード長 1,259 bytes
コンパイル時間 257 ms
コンパイル使用メモリ 82,492 KB
実行使用メモリ 104,964 KB
最終ジャッジ日時 2024-05-01 02:55:59
合計ジャッジ時間 3,861 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
52,924 KB
testcase_01 AC 34 ms
52,960 KB
testcase_02 AC 33 ms
54,104 KB
testcase_03 AC 32 ms
53,012 KB
testcase_04 AC 34 ms
53,148 KB
testcase_05 AC 33 ms
52,516 KB
testcase_06 AC 32 ms
52,904 KB
testcase_07 AC 35 ms
53,616 KB
testcase_08 AC 33 ms
52,264 KB
testcase_09 AC 55 ms
84,624 KB
testcase_10 AC 56 ms
84,568 KB
testcase_11 AC 55 ms
84,508 KB
testcase_12 AC 34 ms
52,692 KB
testcase_13 AC 31 ms
53,668 KB
testcase_14 AC 33 ms
52,748 KB
testcase_15 AC 38 ms
55,932 KB
testcase_16 AC 34 ms
53,144 KB
testcase_17 AC 36 ms
54,332 KB
testcase_18 AC 44 ms
58,556 KB
testcase_19 AC 182 ms
94,468 KB
testcase_20 AC 156 ms
94,748 KB
testcase_21 AC 153 ms
85,132 KB
testcase_22 AC 82 ms
86,960 KB
testcase_23 AC 136 ms
88,820 KB
testcase_24 AC 224 ms
104,964 KB
testcase_25 AC 172 ms
103,096 KB
testcase_26 AC 232 ms
104,740 KB
testcase_27 AC 138 ms
97,588 KB
testcase_28 AC 208 ms
101,728 KB
testcase_29 AC 176 ms
97,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from _collections import defaultdict
n,k=map(int,input().split())
root=[[] for i in range(n+10)]

class topological_sort():
    #1-indexed
    #0-indexedはバグる
    def __init__(self,n):
        self.n=n
        self.root=[[] for _ in range(self.n+1)]
        self.deg=[0]*(self.n+1)
    def add(self,a,b):
        #有向辺 a->b の追加
        self.root[a].append(b)
        self.deg[b]+=1
    def const(self):
        #トポロジカルソートした結果を返す
        #不可能なら [] を返す
        deg2=self.deg[:]
        search=[]
        res=[]
        for i in range(1,self.n+1):
            if deg2[i]==0:search.append(i)
        while search:
            now=search.pop()
            res.append(now)
            for next in self.root[now]:
                deg2[next]-=1
                if deg2[next]==0:
                    search.append(next)
        if len(res)==self.n:
            return res
        return []

tp=topological_sort(n)

for i in range(k):
    a,b=map(int,input().split())
    root[a].append(b)
    tp.add(a,b)

tps=tp.const()
if len(tps)==0:
    print(-1)
    exit()

ans=0
dp=[0]*(n+1)
tps.reverse()
for x in tps:
    for y in root[x]:
        dp[x]=max(dp[x],dp[y]+1)
    ans=max(ans,dp[x])

print(ans+1)
0