結果

問題 No.1660 Matrix Exponentiation
ユーザー ああいいああいい
提出日時 2022-04-27 10:09:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 520 ms / 2,000 ms
コード長 3,741 bytes
コンパイル時間 324 ms
コンパイル使用メモリ 86,876 KB
実行使用メモリ 149,000 KB
最終ジャッジ日時 2023-09-10 16:32:20
合計ジャッジ時間 9,068 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,300 KB
testcase_01 AC 74 ms
71,256 KB
testcase_02 AC 75 ms
71,196 KB
testcase_03 AC 75 ms
71,316 KB
testcase_04 AC 75 ms
71,436 KB
testcase_05 AC 73 ms
71,108 KB
testcase_06 AC 74 ms
71,436 KB
testcase_07 AC 75 ms
71,224 KB
testcase_08 AC 73 ms
71,164 KB
testcase_09 AC 166 ms
105,052 KB
testcase_10 AC 140 ms
105,116 KB
testcase_11 AC 78 ms
75,888 KB
testcase_12 AC 73 ms
71,108 KB
testcase_13 AC 73 ms
71,380 KB
testcase_14 AC 75 ms
71,440 KB
testcase_15 AC 82 ms
71,296 KB
testcase_16 AC 76 ms
71,196 KB
testcase_17 AC 75 ms
71,168 KB
testcase_18 AC 91 ms
75,528 KB
testcase_19 AC 417 ms
118,684 KB
testcase_20 AC 367 ms
117,140 KB
testcase_21 AC 334 ms
96,152 KB
testcase_22 AC 172 ms
100,996 KB
testcase_23 AC 303 ms
106,460 KB
testcase_24 AC 512 ms
149,000 KB
testcase_25 AC 353 ms
140,636 KB
testcase_26 AC 520 ms
148,164 KB
testcase_27 AC 78 ms
75,828 KB
testcase_28 AC 503 ms
132,608 KB
testcase_29 AC 375 ms
113,920 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N,K = map(int,input().split())
G = [[] for _ in range(N)]
import sys
for _ in range(K):
    r,c = map(int,input().split())
    r -= 1
    c -= 1
    G[c].append(r)
    if r == c:
        print(-1)
        exit()

#強連結成分分解
#分解したら、sG を 頂点数num のグラフとして普通に扱えばいい
#sG は set で作ってある
#Componentに各成分を構成するもとの頂点を入れてる
class SCC():

    #index :0-index or 1-index
    #逆グラフは自動で作る
    def __init__(self,G,index = 1):
        self.G = G
        self.N = len(G)
        self.index = index
        self.rG = self.make_reverse()
        self.parent = [0] * self.N  #各頂点の属する強連結成分の番号

        self.num = 0     #強連結成分の数
        
        self.build()
        self.sG,self.top,self.Component = self.make_sG()
        
        #sG 縮約後のグラフ。setでつくってある
        #top sGをトポロジカルソートしたもの
        #Component 各強連結成分に属する頂点をまとめた配列

    #逆グラフを作る
    def make_reverse(self):
        G = self.G
        rG = [[] for _ in range(self.N)]
        for v in range(self.index,self.N):
            for u in G[v]:
                rG[u].append(v)
        return rG
    def build(self):
        G = self.G
        rG = self.rG
        parent = self.parent
        
        order = []
        memo = [0] * self.N
        stack = []
        label = 0
        for v in range(self.index,self.N):
            if memo[v] == 1:continue
            stack.append((v,0))
            memo[v] = 1
            while stack:
                now,i = stack.pop()
                while i < len(G[now]):
                    u = G[now][i]
                    if memo[u] == 0:
                        memo[u] = 1
                        stack.append((now,i+1))
                        stack.append((u,0))
                        break
                    i += 1
                if i == len(G[now]):
                    order.append(now)
                        
        memo = [0] * self.N
        for v in reversed(order):
            if memo[v] == 1:continue
            stack.append(v)
            memo[v] = 1
            parent[v] = label
            while stack:
                now = stack.pop()
                for u in rG[now]:
                    if memo[u] == 1:
                        continue
                    memo[u] = 1
                    stack.append(u)
                    parent[u] = label
            label += 1
        self.num = label
    #縮約後のグラフを作る
    #トポロジカルソートもする
    def make_sG(self):
        sG = [set() for _ in range(self.num)]
        sGnum = [0] * self.num
        Component = [[] for _ in range(self.num)]
        G = self.G
        parent = self.parent
        for v in range(self.index,self.N):
            pv = parent[v]
            Component[pv].append(v)
            for u in G[v]:
                pu = parent[u]
                if pu == pv:continue
                if pu not in sG[pv]:
                    sG[pv].add(pu)
                    sGnum[pu] += 1
        top = []
        stack = []
        for i in range(self.num):
            if sGnum[i] == 0:
                stack.append(i)
        while stack:
            now = stack.pop()
            top.append(now)
            for v in sG[now]:
                sGnum[v] -= 1
                if sGnum[v] == 0:
                    stack.append(v)
        return sG,top,Component


scc = SCC(G,0)
n = scc.num

if n < N:
    print(-1)
    exit()
top = scc.top
sG = scc.sG
dp = [0] * n
for i in top:
    for j in sG[i]:
        dp[j] = max(dp[j],dp[i] + 1)
ans = max(dp)
print(ans + 1)
0