結果
問題 | No.1612 I hate Construct a Palindrome |
ユーザー | Kude |
提出日時 | 2021-07-21 22:36:12 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 712 ms / 2,000 ms |
コード長 | 1,571 bytes |
コンパイル時間 | 195 ms |
コンパイル使用メモリ | 82,488 KB |
実行使用メモリ | 150,060 KB |
最終ジャッジ日時 | 2024-07-17 19:35:54 |
合計ジャッジ時間 | 10,723 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 38 ms
55,548 KB |
testcase_01 | AC | 82 ms
77,208 KB |
testcase_02 | AC | 520 ms
150,060 KB |
testcase_03 | AC | 37 ms
55,052 KB |
testcase_04 | AC | 77 ms
77,164 KB |
testcase_05 | AC | 552 ms
143,420 KB |
testcase_06 | AC | 476 ms
120,260 KB |
testcase_07 | AC | 483 ms
119,052 KB |
testcase_08 | AC | 487 ms
118,596 KB |
testcase_09 | AC | 405 ms
110,800 KB |
testcase_10 | AC | 409 ms
112,116 KB |
testcase_11 | AC | 404 ms
111,896 KB |
testcase_12 | AC | 441 ms
124,268 KB |
testcase_13 | AC | 485 ms
123,988 KB |
testcase_14 | AC | 459 ms
123,916 KB |
testcase_15 | AC | 712 ms
137,844 KB |
testcase_16 | AC | 697 ms
138,492 KB |
testcase_17 | AC | 662 ms
137,532 KB |
testcase_18 | AC | 62 ms
68,952 KB |
testcase_19 | AC | 67 ms
69,064 KB |
testcase_20 | AC | 65 ms
69,044 KB |
testcase_21 | AC | 71 ms
75,560 KB |
testcase_22 | AC | 71 ms
74,976 KB |
testcase_23 | AC | 71 ms
74,536 KB |
testcase_24 | AC | 74 ms
77,700 KB |
testcase_25 | AC | 73 ms
77,500 KB |
testcase_26 | AC | 70 ms
75,200 KB |
testcase_27 | AC | 39 ms
54,620 KB |
testcase_28 | AC | 37 ms
54,708 KB |
testcase_29 | AC | 37 ms
54,420 KB |
testcase_30 | AC | 37 ms
55,200 KB |
testcase_31 | AC | 41 ms
54,512 KB |
testcase_32 | AC | 38 ms
53,932 KB |
testcase_33 | AC | 38 ms
54,748 KB |
testcase_34 | AC | 37 ms
55,868 KB |
testcase_35 | AC | 37 ms
54,296 KB |
testcase_36 | AC | 37 ms
54,512 KB |
testcase_37 | AC | 39 ms
55,312 KB |
testcase_38 | AC | 36 ms
54,484 KB |
ソースコード
from collections import deque n, m = map(int, input().split()) to = [[] for _ in range(n)] for i in range(1, m + 1): a, b, c = input().split() a = int(a) - 1 b = int(b) - 1 c = ord(c) - 97 to[a].append((i, b, c)) to[b].append((i, a, c)) z = -1 for i in range(n): seen = 0 for _, _, c in to[i]: seen |= 1 << c if bin(seen).count('1') >= 2: z = i break if z == -1: print(-1) exit() INF = 10 ** 9 def shortest_path(s, t): pre = [None] * n dist = [INF] * n q = deque() dist[s] = 0 q.append(s) while q: u = q.popleft() for i, v, c in to[u]: if dist[v] == INF: dist[v] = dist[u] + 1 pre[v] = (u, i, c) q.append(v) u = t path = [] s = [] while pre[u] is not None: u, i, c = pre[u] path.append(i) s.append(c) s.reverse() path.reverse() return path, s p1, s1 = shortest_path(0, z) p2, s2 = shortest_path(n - 1, z) s = s1 + s2[::-1] if s == s[::-1]: if s1 == s2: last_edge = p1[-1] last_col = s1[-1] for i, _, c in to[z]: if c != last_col: break p1 += last_edge, last_edge, i, i else: seen = [False] * 26 for i, _, c in to[z]: if seen[c]: continue seen[c] = True t = s1 + [c, c] + s2[::-1] if t != t[::-1]: p1 += i, i break p = p1 + p2[::-1] print(len(p)) print(*p, sep='\n')