結果

問題 No.1660 Matrix Exponentiation
ユーザー 👑 KazunKazun
提出日時 2021-08-27 22:26:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 215 ms / 2,000 ms
コード長 1,535 bytes
コンパイル時間 305 ms
コンパイル使用メモリ 82,700 KB
実行使用メモリ 112,416 KB
最終ジャッジ日時 2024-05-01 03:07:49
合計ジャッジ時間 3,826 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,372 KB
testcase_01 AC 34 ms
53,204 KB
testcase_02 AC 35 ms
52,836 KB
testcase_03 AC 36 ms
52,692 KB
testcase_04 AC 34 ms
53,024 KB
testcase_05 AC 35 ms
53,640 KB
testcase_06 AC 37 ms
53,980 KB
testcase_07 AC 33 ms
52,748 KB
testcase_08 AC 35 ms
54,168 KB
testcase_09 AC 62 ms
90,324 KB
testcase_10 AC 63 ms
90,012 KB
testcase_11 AC 61 ms
86,524 KB
testcase_12 AC 35 ms
53,892 KB
testcase_13 AC 35 ms
53,244 KB
testcase_14 AC 35 ms
53,392 KB
testcase_15 AC 38 ms
54,920 KB
testcase_16 AC 35 ms
54,448 KB
testcase_17 AC 35 ms
53,896 KB
testcase_18 AC 37 ms
55,000 KB
testcase_19 AC 187 ms
99,108 KB
testcase_20 AC 154 ms
99,404 KB
testcase_21 AC 141 ms
88,024 KB
testcase_22 AC 68 ms
84,688 KB
testcase_23 AC 123 ms
91,404 KB
testcase_24 AC 207 ms
112,124 KB
testcase_25 AC 155 ms
111,856 KB
testcase_26 AC 199 ms
112,416 KB
testcase_27 AC 116 ms
104,156 KB
testcase_28 AC 215 ms
107,820 KB
testcase_29 AC 158 ms
104,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Topological_Sort:
    def __init__(self, N: int):
        """ N 頂点からなる空グラフを用意する.

        N: int
        """
        self.N=N
        self.arc=[[] for _ in  range(N)]
        self.rev=[[] for _ in range(N)]

    def add_arc(self, source: int, target: int):
        """ 有向辺 source → taeget を追加する.

        """
        self.arc[source].append(target)
        self.rev[target].append(source)

    def sort(self):
        """ トポロジカルソートを求める.

        [Ouput]
        存在する → トポロジカルソート
        存在しない → None
        """

        in_deg=[len(self.rev[x]) for x in range(self.N)]
        Q=[x for x in range(self.N) if in_deg[x]==0]

        S=[]
        while Q:
            u=Q.pop()
            S.append(u)

            for v in self.arc[u]:
                in_deg[v]-=1
                if in_deg[v]==0:
                    Q.append(v)

        return S if len(S)==self.N else None

    def is_DAG(self):
        """ DAG かどうかを判定する.
        """
        return self.sort()!=None
#==================================================
import sys

input=sys.stdin.readline
N,K=map(int,input().split())

T=Topological_Sort(N)
A=[[] for _ in range(N)]
for _ in range(K):
    r,c=map(int,input().split())
    r-=1; c-=1

    T.add_arc(r,c)
    A[r].append(c)

X=T.sort()

if X==None:
    exit(print(-1))

DP=[0]*N
for x in X[::-1]:
    m=0
    for y in A[x]:
        m=max(m,DP[y]+1)
    DP[x]=m

print(max(DP)+1)
0