結果

問題 No.1660 Matrix Exponentiation
ユーザー puznekopuzneko
提出日時 2021-11-26 00:33:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 269 ms / 2,000 ms
コード長 1,373 bytes
コンパイル時間 273 ms
コンパイル使用メモリ 86,848 KB
実行使用メモリ 116,364 KB
最終ジャッジ日時 2023-09-11 02:43:06
合計ジャッジ時間 5,451 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,148 KB
testcase_01 AC 73 ms
71,244 KB
testcase_02 AC 73 ms
71,108 KB
testcase_03 AC 71 ms
71,188 KB
testcase_04 AC 70 ms
71,136 KB
testcase_05 AC 71 ms
71,048 KB
testcase_06 AC 71 ms
71,060 KB
testcase_07 AC 73 ms
71,224 KB
testcase_08 AC 71 ms
71,364 KB
testcase_09 AC 106 ms
84,060 KB
testcase_10 AC 108 ms
84,236 KB
testcase_11 AC 80 ms
77,444 KB
testcase_12 AC 72 ms
71,204 KB
testcase_13 AC 72 ms
70,972 KB
testcase_14 AC 71 ms
71,180 KB
testcase_15 AC 84 ms
76,380 KB
testcase_16 AC 73 ms
71,084 KB
testcase_17 AC 75 ms
71,080 KB
testcase_18 AC 90 ms
76,372 KB
testcase_19 AC 231 ms
109,212 KB
testcase_20 AC 206 ms
100,116 KB
testcase_21 AC 196 ms
114,164 KB
testcase_22 AC 121 ms
81,852 KB
testcase_23 AC 180 ms
90,824 KB
testcase_24 AC 269 ms
116,236 KB
testcase_25 AC 202 ms
114,928 KB
testcase_26 AC 263 ms
116,364 KB
testcase_27 AC 136 ms
116,264 KB
testcase_28 AC 151 ms
116,296 KB
testcase_29 AC 173 ms
116,232 KB
権限があれば一括ダウンロードができます

ソースコード

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)]
visited = [False for i in range(n+1)]
for i in range(1,n+1):
    if not check[i]:
        que = [(i,0)]
        while que:
            now, inout = que.pop()
            if inout == 0:
                if not check[now]:
                    check[now] = True
                    que.append((now,1))
                    visited[now] = True
                    for j in g[now]:
                        if visited[j]:
                            print(-1)
                            exit()
                        que.append((j,0))
            else:
                visited[now] = False

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)]
        while que:
            now, inout = que.pop()
            if inout == 0:
                if not check[now]:
                    check[now] = True
                    que.append((now,1))
                    for j in g[now]:
                        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