結果

問題 No.1612 I hate Construct a Palindrome
ユーザー ayaoniayaoni
提出日時 2021-07-21 22:40:13
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,686 bytes
コンパイル時間 522 ms
コンパイル使用メモリ 86,972 KB
実行使用メモリ 184,412 KB
最終ジャッジ日時 2023-09-24 18:57:01
合計ジャッジ時間 17,097 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 409 ms
128,264 KB
testcase_10 AC 432 ms
128,116 KB
testcase_11 AC 460 ms
130,440 KB
testcase_12 AC 504 ms
137,940 KB
testcase_13 AC 500 ms
137,228 KB
testcase_14 WA -
testcase_15 AC 726 ms
168,484 KB
testcase_16 WA -
testcase_17 AC 663 ms
169,820 KB
testcase_18 WA -
testcase_19 AC 107 ms
77,120 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 111 ms
77,988 KB
testcase_23 WA -
testcase_24 AC 113 ms
77,776 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 97 ms
71,632 KB
testcase_28 AC 95 ms
71,672 KB
testcase_29 AC 93 ms
71,480 KB
testcase_30 AC 95 ms
71,628 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 92 ms
71,556 KB
testcase_36 AC 96 ms
71,624 KB
testcase_37 WA -
testcase_38 WA -
権限があれば一括ダウンロードができます

ソースコード

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]
s = goal
while s != N:
    d,i,u = dist[s]
    ANS1.append(i)
    s = 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