結果

問題 No.1612 I hate Construct a Palindrome
ユーザー ayaoniayaoni
提出日時 2021-07-21 22:45:53
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,690 bytes
コンパイル時間 1,927 ms
コンパイル使用メモリ 86,672 KB
実行使用メモリ 169,608 KB
最終ジャッジ日時 2023-09-24 19:03:46
合計ジャッジ時間 15,164 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 369 ms
127,948 KB
testcase_10 AC 398 ms
128,072 KB
testcase_11 AC 423 ms
130,436 KB
testcase_12 AC 492 ms
137,916 KB
testcase_13 AC 472 ms
137,124 KB
testcase_14 WA -
testcase_15 AC 708 ms
168,328 KB
testcase_16 AC 680 ms
169,444 KB
testcase_17 AC 640 ms
169,608 KB
testcase_18 AC 95 ms
72,460 KB
testcase_19 AC 99 ms
77,164 KB
testcase_20 AC 100 ms
77,100 KB
testcase_21 AC 106 ms
77,864 KB
testcase_22 AC 100 ms
77,616 KB
testcase_23 AC 105 ms
77,852 KB
testcase_24 AC 103 ms
77,908 KB
testcase_25 AC 111 ms
77,656 KB
testcase_26 WA -
testcase_27 AC 89 ms
71,680 KB
testcase_28 AC 92 ms
71,676 KB
testcase_29 AC 88 ms
71,444 KB
testcase_30 AC 90 ms
71,816 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 87 ms
71,448 KB
testcase_34 AC 87 ms
71,516 KB
testcase_35 AC 92 ms
71,608 KB
testcase_36 AC 88 ms
71,668 KB
testcase_37 AC 86 ms
71,324 KB
testcase_38 AC 85 ms
71,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque
sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def LS2(): return list(sys.stdin.readline().rstrip())


N,M = MI()
Graph = [[] for _ in range(N+1)]
edges = []
char = set()
for i in range(1,M+1):
    A,B,C = S().split()
    A = int(A)
    B = int(B)
    Graph[A].append((B,C,i))
    Graph[B].append((A,C,i))
    edges.append((A,B,C))
    char.add(C)

if len(char) == 1:
    print(-1)
    exit()

s,a,idx = Graph[1][0]

for i in range(M):
    A,B,C = edges[i]
    if a != C:
        goal = A
        e = i+1
        goal2 = B

inf = 10**18
dist = [inf]*(N+1)
dist[N] = (0,-1,-1)
deq = deque([N])
while deq:
    u = deq.pop()
    if u == goal:
        break
    for v,_,i in Graph[u]:
        if dist[v] == inf:
            dist[v] = (dist[u][0]+1,i,u)
            deq.appendleft(v)

ANS1 = [e]
ss = goal
while ss != N:
    d,i,u = dist[ss]
    ANS1.append(i)
    ss = u

ANS0 = [idx]*len(ANS1)
if len(ANS0) % 2 == 1:
    cur = s
else:
    cur = 1

dist = [inf]*(N+1)
dist[B] = (0,-1,-1)
deq = deque([B])
while deq:
    u = deq.pop()
    if u == cur:
        break
    for v,_,i in Graph[u]:
        if dist[v] == inf:
            dist[v] = (dist[u][0]+1,i,u)
            deq.appendleft(v)

s = cur
while s != B:
    d,i,u = dist[s]
    ANS0.append(i)
    s = u

ANS = ANS0+ANS1
print(len(ANS))
print(*ANS,sep='\n')
0