結果

問題 No.1660 Matrix Exponentiation
ユーザー mkawa2mkawa2
提出日時 2023-03-06 14:20:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 305 ms / 2,000 ms
コード長 2,091 bytes
コンパイル時間 213 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 118,484 KB
最終ジャッジ日時 2023-10-18 05:09:28
合計ジャッジ時間 6,243 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,452 KB
testcase_01 AC 39 ms
53,452 KB
testcase_02 AC 38 ms
53,452 KB
testcase_03 AC 39 ms
53,452 KB
testcase_04 AC 38 ms
53,452 KB
testcase_05 AC 37 ms
53,452 KB
testcase_06 AC 43 ms
53,452 KB
testcase_07 AC 38 ms
53,452 KB
testcase_08 AC 38 ms
53,452 KB
testcase_09 AC 39 ms
53,452 KB
testcase_10 AC 89 ms
93,208 KB
testcase_11 AC 46 ms
64,628 KB
testcase_12 AC 39 ms
53,452 KB
testcase_13 AC 39 ms
53,452 KB
testcase_14 AC 39 ms
53,452 KB
testcase_15 AC 47 ms
61,228 KB
testcase_16 AC 41 ms
53,452 KB
testcase_17 AC 40 ms
53,452 KB
testcase_18 AC 54 ms
64,156 KB
testcase_19 AC 273 ms
99,432 KB
testcase_20 AC 217 ms
95,164 KB
testcase_21 AC 210 ms
85,356 KB
testcase_22 AC 94 ms
87,276 KB
testcase_23 AC 166 ms
88,720 KB
testcase_24 AC 304 ms
118,484 KB
testcase_25 AC 234 ms
109,956 KB
testcase_26 AC 305 ms
118,472 KB
testcase_27 AC 46 ms
64,628 KB
testcase_28 AC 297 ms
105,392 KB
testcase_29 AC 302 ms
117,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = (1 << 63)-1
# inf = (1 << 31)-1
md = 10**9+7
# md = 998244353

def SCC(to, ot):
    n = len(to)
    fin = [-1]*n
    topo = []
    for u in range(n):
        if fin[u] != -1: continue
        stack = [u]
        while stack:
            u = stack[-1]
            if fin[u] == -1:
                fin[u] = 0
                for v in to[u]:
                    if fin[v] != -1: continue
                    stack.append(v)
            else:
                stack.pop()
                if fin[u] == 0:
                    fin[u] = 1
                    topo.append(u)
    res = []
    while topo:
        u = topo.pop()
        if fin[u] != 1: continue
        fin[u] = 2
        cur = [u]
        i = 0
        while i < len(cur):
            u = cur[i]
            for v in ot[u]:
                if fin[v] == 2: continue
                fin[v] = 2
                cur.append(v)
            i += 1
        res.append(cur)

    return res

n, k = LI()

if k == 0:
    print(1)
    exit()

to = [[] for _ in range(n)]
ot = [[] for _ in range(n)]
for _ in range(k):
    u, v = LI1()
    if u==v:
        print(-1)
        exit()
    to[u].append(v)
    ot[v].append(u)

gg = SCC(to, ot)
if any(len(g) > 1 for g in gg):
    print(-1)
else:
    dist = [1]*n
    ans = 1
    for g in gg[::-1]:
        u = g[0]
        for v in to[u]:
            dist[u] = max(dist[u], dist[v]+1)
        if dist[u] > ans: ans = dist[u]
    print(ans)
0