結果
| 問題 |
No.1612 I hate Construct a Palindrome
|
| コンテスト | |
| ユーザー |
gew1fw
|
| 提出日時 | 2025-06-12 21:21:09 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 4,324 bytes |
| コンパイル時間 | 156 ms |
| コンパイル使用メモリ | 82,480 KB |
| 実行使用メモリ | 172,000 KB |
| 最終ジャッジ日時 | 2025-06-12 21:22:27 |
| 合計ジャッジ時間 | 9,187 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 26 WA * 10 |
ソースコード
import sys
from collections import deque
def main():
input = sys.stdin.read().split()
ptr = 0
N = int(input[ptr])
ptr += 1
M = int(input[ptr])
ptr += 1
edges = []
adj = [[] for _ in range(N+1)] # 1-based
for i in range(M):
A = int(input[ptr])
ptr += 1
B = int(input[ptr])
ptr += 1
C = input[ptr]
ptr += 1
edges.append((A, B, C))
adj[A].append((B, C, i))
adj[B].append((A, C, i))
# BFS from 1 to compute parent_1
parent_1 = [-1] * (N + 1)
visited = [False] * (N + 1)
q = deque([1])
visited[1] = True
while q:
u = q.popleft()
for v, c, idx in adj[u]:
if not visited[v]:
visited[v] = True
parent_1[v] = idx
q.append(v)
# Reconstruct the shortest path from 1 to N
path_1_to_N = []
current = N
if not visited[N]:
print(-1)
return
while current != 1:
edge_idx = parent_1[current]
if edge_idx == -1:
print(-1)
return
path_1_to_N.append(edge_idx)
a, b, c = edges[edge_idx]
if a == current:
current = b
else:
current = a
path_1_to_N.reverse()
if not path_1_to_N:
print(-1)
return
first_edge_idx = path_1_to_N[0]
last_edge_idx = path_1_to_N[-1]
first_char = edges[first_edge_idx][2]
last_char = edges[last_edge_idx][2]
if first_char != last_char:
print(len(path_1_to_N))
for idx in path_1_to_N:
print(idx + 1)
return
# Need to find another path
c = first_char
# Check edges from 1 with label != c
found = False
for v, c_label, idx in adj[1]:
if c_label != c:
# Reconstruct path from v to 1
path_v_to_1 = []
current = v
valid = True
while current != 1:
edge_idx = parent_1[current]
if edge_idx == -1:
valid = False
break
path_v_to_1.append(edge_idx)
a, b, c_e = edges[edge_idx]
if a == current:
current = b
else:
current = a
if not valid:
continue
# Reconstruct path from 1 to N again
path_1_to_N_current = []
current = N
while current != 1:
edge_idx = parent_1[current]
path_1_to_N_current.append(edge_idx)
a, b, c_e = edges[edge_idx]
if a == current:
current = b
else:
current = a
path_1_to_N_current.reverse()
full_path = [idx] + path_v_to_1 + path_1_to_N_current
if len(full_path) > 2 * N:
continue
print(len(full_path))
for p in full_path:
print(p + 1)
return
# Check edges to N with label != c
for v, c_label, idx in adj[N]:
if c_label != c:
# Reconstruct path from 1 to v
path_1_to_v = []
current = v
valid = True
while current != 1:
edge_idx = parent_1[current]
if edge_idx == -1:
valid = False
break
path_1_to_v.append(edge_idx)
a, b, c_e = edges[edge_idx]
if a == current:
current = b
else:
current = a
if not valid:
continue
path_1_to_v.reverse()
# Check first edge of path_1_to_v
if not path_1_to_v:
first_char_v = c_label # path is 1 to v directly via edge from 1 to v?
else:
first_char_v = edges[path_1_to_v[0]][2]
if first_char_v != c_label:
full_path = path_1_to_v + [idx]
if len(full_path) > 2 * N:
continue
print(len(full_path))
for p in full_path:
print(p + 1)
return
print(-1)
if __name__ == "__main__":
main()
gew1fw