結果

問題 No.1660 Matrix Exponentiation
ユーザー ygd.ygd.
提出日時 2021-08-29 11:47:28
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,333 bytes
コンパイル時間 174 ms
コンパイル使用メモリ 82,196 KB
実行使用メモリ 96,468 KB
最終ジャッジ日時 2024-05-01 21:49:56
合計ジャッジ時間 3,848 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
55,240 KB
testcase_01 AC 43 ms
54,208 KB
testcase_02 AC 43 ms
54,596 KB
testcase_03 AC 43 ms
54,660 KB
testcase_04 WA -
testcase_05 AC 43 ms
54,468 KB
testcase_06 AC 43 ms
55,244 KB
testcase_07 AC 43 ms
54,780 KB
testcase_08 AC 43 ms
55,584 KB
testcase_09 AC 43 ms
54,980 KB
testcase_10 WA -
testcase_11 AC 48 ms
63,020 KB
testcase_12 AC 43 ms
54,436 KB
testcase_13 AC 45 ms
54,796 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 135 ms
96,468 KB
testcase_26 WA -
testcase_27 AC 48 ms
64,488 KB
testcase_28 AC 183 ms
91,428 KB
testcase_29 AC 125 ms
85,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

from collections import defaultdict 

def main():
    n,k = map(int,input().split()); MOD = pow(10,9) + 7
    if k == 0:
        print(1);exit()
    #A = [[0]*n for _ in range(n)]
    G = [[] for _ in range(n)]
    deg = [0]*n
    dic = {}
    for _ in range(k):
        r,c = map(int,input().split())
        r -= 1; c -= 1
        if r == c: #対角成分に1があったら無理
            print(-1);exit()
        G[r].append(c)
        deg[c] += 1
    
    TP = topological(G,deg)
    #print(TP)
    if len(TP) < n:
        print(-1);exit()
    
    dp = [0]*n
    for v in deg:
        dp[v] = 1
    for v in TP:
        for u in G[v]:
            dp[u] = max(dp[u], dp[v] + 1)
    #print(dp)
    ans = max(dp)
    print(ans)

    
    

# degは入次数を記録したもの
def topological(graph, deg):
    start = []
    n = len(deg)
    for i in range(n):
        if deg[i] == 0: #入次数がないものがスタート
            start.append(i)
    topo = []
    while start:
        v = start.pop()
        topo.append(v)
        for u in graph[v]:
            deg[u] -= 1
            if deg[u] == 0:
                start.append(u)
    #トポロジカルソートできない場合は配列がnよりも短くなる。
    return topo


if __name__ == "__main__":
    main()
0