結果

問題 No.1660 Matrix Exponentiation
ユーザー rlangevinrlangevin
提出日時 2023-10-05 00:08:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 218 ms / 2,000 ms
コード長 864 bytes
コンパイル時間 395 ms
コンパイル使用メモリ 87,068 KB
実行使用メモリ 95,748 KB
最終ジャッジ日時 2023-10-05 00:08:58
合計ジャッジ時間 5,708 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
71,920 KB
testcase_01 AC 87 ms
71,716 KB
testcase_02 AC 86 ms
71,956 KB
testcase_03 AC 86 ms
71,624 KB
testcase_04 AC 86 ms
71,724 KB
testcase_05 AC 87 ms
71,900 KB
testcase_06 AC 90 ms
71,668 KB
testcase_07 AC 86 ms
71,616 KB
testcase_08 AC 85 ms
71,824 KB
testcase_09 AC 114 ms
86,224 KB
testcase_10 AC 108 ms
86,204 KB
testcase_11 AC 93 ms
77,244 KB
testcase_12 AC 85 ms
71,876 KB
testcase_13 AC 87 ms
71,432 KB
testcase_14 AC 88 ms
71,428 KB
testcase_15 AC 98 ms
72,076 KB
testcase_16 AC 87 ms
71,756 KB
testcase_17 AC 88 ms
71,460 KB
testcase_18 AC 99 ms
72,072 KB
testcase_19 AC 190 ms
89,152 KB
testcase_20 AC 191 ms
89,164 KB
testcase_21 AC 164 ms
82,600 KB
testcase_22 AC 115 ms
83,084 KB
testcase_23 AC 154 ms
86,064 KB
testcase_24 AC 200 ms
95,748 KB
testcase_25 AC 152 ms
91,560 KB
testcase_26 AC 218 ms
95,332 KB
testcase_27 AC 101 ms
77,212 KB
testcase_28 AC 202 ms
94,796 KB
testcase_29 AC 149 ms
88,404 KB
権限があれば一括ダウンロードができます

ソースコード

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)
if len(A) != N:
    print(-1)
    exit()
    
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