結果

問題 No.1660 Matrix Exponentiation
ユーザー puznekopuzneko
提出日時 2021-11-26 00:20:00
言語 PyPy3
(7.3.13)
結果
TLE  
実行時間 -
コード長 1,337 bytes
コンパイル時間 722 ms
コンパイル使用メモリ 86,716 KB
実行使用メモリ 195,744 KB
最終ジャッジ日時 2023-09-11 02:29:48
合計ジャッジ時間 10,049 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,372 KB
testcase_01 AC 72 ms
71,464 KB
testcase_02 AC 71 ms
71,228 KB
testcase_03 AC 73 ms
71,392 KB
testcase_04 AC 73 ms
71,084 KB
testcase_05 AC 74 ms
70,924 KB
testcase_06 AC 75 ms
71,284 KB
testcase_07 AC 73 ms
71,264 KB
testcase_08 AC 74 ms
71,264 KB
testcase_09 AC 117 ms
83,392 KB
testcase_10 AC 115 ms
83,100 KB
testcase_11 AC 77 ms
76,564 KB
testcase_12 AC 71 ms
71,400 KB
testcase_13 AC 72 ms
71,112 KB
testcase_14 AC 74 ms
71,108 KB
testcase_15 AC 99 ms
76,684 KB
testcase_16 AC 75 ms
71,404 KB
testcase_17 AC 79 ms
76,312 KB
testcase_18 AC 110 ms
77,668 KB
testcase_19 AC 279 ms
109,200 KB
testcase_20 AC 213 ms
99,528 KB
testcase_21 AC 1,281 ms
114,284 KB
testcase_22 AC 115 ms
81,592 KB
testcase_23 AC 169 ms
91,012 KB
testcase_24 TLE -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import stdin
n, m, *indata = map(int, stdin.read().split())
offset = 0
g = [[] for i in range(n+1)]
for i in range(m):
    s, t = indata[offset + 2*i],indata[offset + 2*i+1]
    g[s].append(t)

check = [False for i in range(n+1)]
for i in range(1,n+1):
    if not check[i]:
        que = [(i,0)]
        visited = set()
        while que:
            now, inout = que.pop()
            if inout == 0:
                if not check[now]:
                    check[now] == True
                    que.append((now,1))
                    visited.add(now)
                    for j in g[now]:
                        if j in visited:
                            print(-1)
                            exit()
                        que.append((j,0))
            else:
                visited.remove(now)

dp = [0 for i in range(n+1)]
check = [False for i in range(n+1)]
for i in range(1,n+1):
    if not check[i]:
        que = [(i,0)]
        check[i] = True
        while que:
            now, inout = que.pop()
            if inout == 0:
                que.append((now,1))
                for j in g[now]:
                    if not check[j]:
                        que.append((j,0))
            else:
                for j in g[now]:
                    dp[now] = max(dp[now],dp[j]+1)

ans = max(dp) + 1
print("{}".format(ans))
0