結果
問題 | No.1612 I hate Construct a Palindrome |
ユーザー |
![]() |
提出日時 | 2024-06-13 23:53:27 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 802 ms / 2,000 ms |
コード長 | 1,692 bytes |
コンパイル時間 | 648 ms |
コンパイル使用メモリ | 82,524 KB |
実行使用メモリ | 164,492 KB |
最終ジャッジ日時 | 2024-06-13 23:53:42 |
合計ジャッジ時間 | 12,487 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 36 |
ソースコード
import sys# sys.setrecursionlimit(1000006)# sys.set_int_max_str_digits(200005)int1 = lambda x: int(x)-1pDB = 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-(-1 << 31)inf = -1-(-1 << 62)# md = 10**9+7md = 998244353from collections import dequen, m = LI()to = [[] for _ in range(n)]cc = []uv = []for i in range(m):u, v, c = SI().split()u = int1(u)v = int1(v)c = ord(c)-97to[u].append((v, i))to[v].append((u, i))cc.append(c)uv.append(u ^ v)q = deque()# dp = [[0]*27 for _ in range(n)]ee = [[None]*27 for _ in range(n)]for v, i in to[0]:c = cc[i]# dp[v][c] = 1ee[v][c] = (0, -1, i)q.append((v, c))while q:u, c = q.popleft()for v, i in to[u]:nc = cif c != cc[i]: nc = 26if ee[v][nc]: continue# dp[v][nc] = 1ee[v][nc] = (u,c,i)q.append((v, nc))# p2D(ee)if ee[-1][-1] == None:print(-1)exit()ii = []u = n-1c = 26while c!=-1:u,c,i = ee[u][c]ii.append(i)s=[cc[i] for i in ii]if s==s[::-1]:ii.append(ii[-1])ii.append(ii[-1])print(len(ii))for i in ii[::-1]:print(i+1)