結果

問題 No.1660 Matrix Exponentiation
ユーザー taiga0629kyoprotaiga0629kyopro
提出日時 2021-08-27 22:17:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 291 ms / 2,000 ms
コード長 1,259 bytes
コンパイル時間 667 ms
コンパイル使用メモリ 86,892 KB
実行使用メモリ 106,540 KB
最終ジャッジ日時 2023-08-13 09:20:35
合計ジャッジ時間 6,033 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,232 KB
testcase_01 AC 72 ms
71,324 KB
testcase_02 AC 71 ms
70,992 KB
testcase_03 AC 74 ms
71,396 KB
testcase_04 AC 72 ms
71,188 KB
testcase_05 AC 72 ms
71,204 KB
testcase_06 AC 72 ms
71,136 KB
testcase_07 AC 73 ms
71,156 KB
testcase_08 AC 72 ms
71,064 KB
testcase_09 AC 95 ms
92,868 KB
testcase_10 AC 95 ms
92,752 KB
testcase_11 AC 92 ms
91,660 KB
testcase_12 AC 72 ms
71,108 KB
testcase_13 AC 71 ms
71,112 KB
testcase_14 AC 72 ms
71,332 KB
testcase_15 AC 79 ms
71,036 KB
testcase_16 AC 73 ms
71,156 KB
testcase_17 AC 73 ms
71,112 KB
testcase_18 AC 83 ms
71,272 KB
testcase_19 AC 261 ms
93,888 KB
testcase_20 AC 209 ms
94,700 KB
testcase_21 AC 209 ms
85,916 KB
testcase_22 AC 123 ms
90,884 KB
testcase_23 AC 175 ms
90,424 KB
testcase_24 AC 291 ms
106,516 KB
testcase_25 AC 223 ms
106,472 KB
testcase_26 AC 283 ms
106,540 KB
testcase_27 AC 187 ms
99,428 KB
testcase_28 AC 282 ms
103,996 KB
testcase_29 AC 233 ms
99,372 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