結果

問題 No.1660 Matrix Exponentiation
ユーザー 👑 KazunKazun
提出日時 2021-08-27 22:26:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 322 ms / 2,000 ms
コード長 1,535 bytes
コンパイル時間 504 ms
コンパイル使用メモリ 87,276 KB
実行使用メモリ 112,776 KB
最終ジャッジ日時 2023-08-13 09:33:52
合計ジャッジ時間 6,458 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,128 KB
testcase_01 AC 76 ms
71,296 KB
testcase_02 AC 77 ms
71,132 KB
testcase_03 AC 78 ms
71,480 KB
testcase_04 AC 76 ms
70,984 KB
testcase_05 AC 79 ms
71,184 KB
testcase_06 AC 77 ms
71,180 KB
testcase_07 AC 75 ms
71,216 KB
testcase_08 AC 74 ms
71,356 KB
testcase_09 AC 104 ms
94,100 KB
testcase_10 AC 105 ms
94,004 KB
testcase_11 AC 99 ms
92,216 KB
testcase_12 AC 76 ms
71,424 KB
testcase_13 AC 74 ms
71,200 KB
testcase_14 AC 74 ms
70,988 KB
testcase_15 AC 77 ms
71,072 KB
testcase_16 AC 75 ms
71,420 KB
testcase_17 AC 74 ms
71,388 KB
testcase_18 AC 78 ms
71,184 KB
testcase_19 AC 278 ms
101,224 KB
testcase_20 AC 232 ms
100,156 KB
testcase_21 AC 218 ms
89,092 KB
testcase_22 AC 122 ms
90,000 KB
testcase_23 AC 189 ms
92,888 KB
testcase_24 AC 300 ms
112,612 KB
testcase_25 AC 209 ms
112,272 KB
testcase_26 AC 300 ms
112,776 KB
testcase_27 AC 164 ms
104,752 KB
testcase_28 AC 322 ms
107,044 KB
testcase_29 AC 242 ms
104,892 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