結果
問題 | No.1660 Matrix Exponentiation |
ユーザー | mkawa2 |
提出日時 | 2023-03-06 14:20:33 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 308 ms / 2,000 ms |
コード長 | 2,091 bytes |
コンパイル時間 | 300 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 119,152 KB |
最終ジャッジ日時 | 2024-09-18 01:49:41 |
合計ジャッジ時間 | 5,235 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
52,736 KB |
testcase_01 | AC | 39 ms
52,224 KB |
testcase_02 | AC | 38 ms
52,352 KB |
testcase_03 | AC | 39 ms
52,736 KB |
testcase_04 | AC | 39 ms
52,736 KB |
testcase_05 | AC | 38 ms
52,096 KB |
testcase_06 | AC | 39 ms
52,224 KB |
testcase_07 | AC | 39 ms
52,224 KB |
testcase_08 | AC | 39 ms
52,736 KB |
testcase_09 | AC | 39 ms
51,968 KB |
testcase_10 | AC | 97 ms
93,756 KB |
testcase_11 | AC | 45 ms
64,128 KB |
testcase_12 | AC | 39 ms
52,608 KB |
testcase_13 | AC | 40 ms
52,864 KB |
testcase_14 | AC | 40 ms
52,608 KB |
testcase_15 | AC | 48 ms
60,416 KB |
testcase_16 | AC | 42 ms
53,888 KB |
testcase_17 | AC | 43 ms
52,992 KB |
testcase_18 | AC | 61 ms
63,872 KB |
testcase_19 | AC | 266 ms
99,712 KB |
testcase_20 | AC | 218 ms
95,448 KB |
testcase_21 | AC | 208 ms
86,144 KB |
testcase_22 | AC | 96 ms
87,936 KB |
testcase_23 | AC | 166 ms
89,248 KB |
testcase_24 | AC | 305 ms
119,152 KB |
testcase_25 | AC | 235 ms
110,284 KB |
testcase_26 | AC | 308 ms
118,784 KB |
testcase_27 | AC | 46 ms
64,256 KB |
testcase_28 | AC | 295 ms
105,856 KB |
testcase_29 | AC | 307 ms
118,144 KB |
ソースコード
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)