結果

問題 No.1612 I hate Construct a Palindrome
ユーザー KudeKude
提出日時 2021-07-21 22:36:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 901 ms / 2,000 ms
コード長 1,571 bytes
コンパイル時間 1,673 ms
コンパイル使用メモリ 86,552 KB
実行使用メモリ 145,504 KB
最終ジャッジ日時 2023-09-24 18:50:08
合計ジャッジ時間 16,017 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
71,484 KB
testcase_01 AC 145 ms
77,820 KB
testcase_02 AC 641 ms
145,504 KB
testcase_03 AC 101 ms
71,788 KB
testcase_04 AC 134 ms
78,472 KB
testcase_05 AC 656 ms
144,860 KB
testcase_06 AC 587 ms
117,040 KB
testcase_07 AC 606 ms
122,340 KB
testcase_08 AC 556 ms
113,648 KB
testcase_09 AC 544 ms
115,640 KB
testcase_10 AC 609 ms
123,764 KB
testcase_11 AC 611 ms
123,756 KB
testcase_12 AC 556 ms
117,308 KB
testcase_13 AC 605 ms
117,108 KB
testcase_14 AC 579 ms
117,240 KB
testcase_15 AC 874 ms
135,892 KB
testcase_16 AC 901 ms
143,204 KB
testcase_17 AC 849 ms
136,244 KB
testcase_18 AC 121 ms
77,176 KB
testcase_19 AC 122 ms
76,956 KB
testcase_20 AC 122 ms
76,972 KB
testcase_21 AC 133 ms
78,488 KB
testcase_22 AC 130 ms
77,792 KB
testcase_23 AC 130 ms
77,872 KB
testcase_24 AC 132 ms
78,484 KB
testcase_25 AC 135 ms
78,324 KB
testcase_26 AC 130 ms
78,212 KB
testcase_27 AC 102 ms
71,652 KB
testcase_28 AC 94 ms
71,468 KB
testcase_29 AC 92 ms
71,680 KB
testcase_30 AC 93 ms
71,684 KB
testcase_31 AC 93 ms
71,556 KB
testcase_32 AC 95 ms
71,544 KB
testcase_33 AC 95 ms
71,456 KB
testcase_34 AC 92 ms
71,596 KB
testcase_35 AC 93 ms
71,592 KB
testcase_36 AC 93 ms
71,668 KB
testcase_37 AC 96 ms
71,512 KB
testcase_38 AC 95 ms
71,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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')
0