結果

問題 No.1660 Matrix Exponentiation
ユーザー rlangevinrlangevin
提出日時 2023-10-04 20:15:11
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 818 bytes
コンパイル時間 344 ms
コンパイル使用メモリ 87,132 KB
実行使用メモリ 95,652 KB
最終ジャッジ日時 2023-10-04 20:15:18
合計ジャッジ時間 6,790 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
71,668 KB
testcase_01 AC 86 ms
71,664 KB
testcase_02 AC 85 ms
71,792 KB
testcase_03 AC 87 ms
71,876 KB
testcase_04 AC 87 ms
71,792 KB
testcase_05 WA -
testcase_06 AC 86 ms
71,848 KB
testcase_07 WA -
testcase_08 AC 86 ms
71,616 KB
testcase_09 AC 105 ms
86,256 KB
testcase_10 AC 133 ms
86,220 KB
testcase_11 AC 92 ms
77,236 KB
testcase_12 AC 88 ms
71,584 KB
testcase_13 AC 87 ms
71,604 KB
testcase_14 AC 88 ms
71,876 KB
testcase_15 AC 92 ms
72,316 KB
testcase_16 AC 88 ms
71,852 KB
testcase_17 AC 88 ms
71,792 KB
testcase_18 AC 93 ms
72,292 KB
testcase_19 AC 192 ms
89,556 KB
testcase_20 AC 194 ms
89,284 KB
testcase_21 AC 163 ms
82,952 KB
testcase_22 AC 112 ms
82,804 KB
testcase_23 AC 155 ms
86,188 KB
testcase_24 AC 200 ms
95,652 KB
testcase_25 AC 152 ms
91,088 KB
testcase_26 AC 192 ms
95,628 KB
testcase_27 AC 99 ms
77,284 KB
testcase_28 WA -
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import *
def topsort(G):
    Q = deque()
    N = len(G)
    count = [0] * N

    for u in range(N):
        for v in G[u]:
            count[v] += 1

    for u in range(N):
        if count[u] == 0:
            Q.append(u)
    anslst = []
    while Q:
        u = Q.pop()
        anslst.append(u)
        for v in G[u]:
            count[v] -= 1
            if count[v] == 0:
                Q.append(v)
    return anslst


N, K = map(int, input().split())   
G = [[] for i in range(N)]
for i in range(K):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    if a == b:
        print(-1)
        exit()
    G[a].append(b)
    
A = topsort(G)
dp = [0] * N
for a in A:
    for u in G[a]:
        dp[u] = max(dp[u], dp[a] + 1)
        
print(max(dp) + 1)
0