結果

問題 No.1612 I hate Construct a Palindrome
ユーザー tamatotamato
提出日時 2021-07-21 22:46:45
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 4,790 bytes
コンパイル時間 1,329 ms
コンパイル使用メモリ 87,416 KB
実行使用メモリ 217,784 KB
最終ジャッジ日時 2023-09-24 19:07:49
合計ジャッジ時間 16,421 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
72,608 KB
testcase_01 AC 137 ms
79,696 KB
testcase_02 AC 880 ms
187,048 KB
testcase_03 AC 89 ms
72,500 KB
testcase_04 AC 131 ms
79,672 KB
testcase_05 AC 910 ms
185,208 KB
testcase_06 WA -
testcase_07 AC 475 ms
149,040 KB
testcase_08 WA -
testcase_09 AC 476 ms
145,256 KB
testcase_10 AC 458 ms
145,228 KB
testcase_11 AC 453 ms
144,860 KB
testcase_12 AC 475 ms
152,884 KB
testcase_13 AC 463 ms
153,092 KB
testcase_14 AC 469 ms
152,904 KB
testcase_15 AC 816 ms
217,784 KB
testcase_16 AC 786 ms
217,660 KB
testcase_17 AC 811 ms
217,656 KB
testcase_18 AC 102 ms
77,352 KB
testcase_19 AC 101 ms
77,108 KB
testcase_20 AC 100 ms
77,008 KB
testcase_21 AC 111 ms
78,388 KB
testcase_22 AC 108 ms
77,912 KB
testcase_23 AC 110 ms
77,900 KB
testcase_24 AC 113 ms
78,060 KB
testcase_25 AC 112 ms
77,904 KB
testcase_26 AC 107 ms
78,116 KB
testcase_27 AC 91 ms
72,508 KB
testcase_28 AC 89 ms
72,216 KB
testcase_29 AC 92 ms
71,784 KB
testcase_30 AC 91 ms
72,740 KB
testcase_31 AC 92 ms
72,632 KB
testcase_32 AC 92 ms
72,688 KB
testcase_33 AC 92 ms
72,808 KB
testcase_34 AC 91 ms
72,488 KB
testcase_35 AC 88 ms
72,524 KB
testcase_36 AC 88 ms
72,812 KB
testcase_37 AC 89 ms
72,832 KB
testcase_38 AC 90 ms
72,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    from collections import deque
    input = sys.stdin.readline

    class UnionFind():
        def __init__(self, n):
            self.n = n
            self.root = [-1] * (n + 1)
            self.rnk = [0] * (n + 1)

        def find_root(self, x):
            while self.root[x] >= 0:
                x = self.root[x]
            return x

        def unite(self, x, y):
            x = self.find_root(x)
            y = self.find_root(y)
            if x == y:
                return
            elif self.rnk[x] > self.rnk[y]:
                self.root[x] += self.root[y]
                self.root[y] = x
            else:
                self.root[y] += self.root[x]
                self.root[x] = y
                if self.rnk[x] == self.rnk[y]:
                    self.rnk[y] += 1

        def isSameGroup(self, x, y):
            return self.find_root(x) == self.find_root(y)

        def size(self, x):
            return -self.root[self.find_root(x)]

    def is_pal(S):
        if S == list(reversed(S)):
            return 1
        else:
            return 0

    N, M = map(int, input().split())
    adj = [[] for _ in range(N+1)]
    S = set()
    E = {}
    I = {}
    ABC = []
    for i in range(M):
        a, b, c = input().split()
        a = int(a)
        b = int(b)
        S.add(c)
        adj[a].append((b, c))
        adj[b].append((a, c))
        E[a * (N+1) + b] = c
        E[b * (N+1) + a] = c
        I[a * (N + 1) + b] = i+1
        I[b * (N + 1) + a] = i+1
        ABC.append((a, b, c))
    if len(S) == 1:
        print(-1)
        exit()

    que = deque()
    que.append(1)
    seen = [-1] * (N+1)
    seen[1] = 0
    par = [0] * (N+1)
    seq = []
    while que:
        v = que.popleft()
        seq.append(v)
        for u, _ in adj[v]:
            if seen[u] == -1:
                seen[u] = seen[v] + 1
                par[u] = v
                que.append(u)

    v = N
    ans = []
    color = []
    while v != 1:
        p = par[v]
        color.append(E[v * (N+1) + p])
        ans.append(I[v * (N+1) + p])
        v = p
    ans.reverse()
    color.reverse()
    if not is_pal(color):
        print(len(ans))
        print(*ans, sep="\n")
        exit()

    for _ in range(2):
        ans.append(ans[-1])
        color.append(color[-1])
    if not is_pal(color):
        print(len(ans))
        print(*ans, sep="\n")
        exit()

    c0 = color[0]
    adj_new = [[] for _ in range(N+1)]
    UF = UnionFind(N)
    for a, b, c in ABC:
        if c == c0:
            adj_new[a].append((b, c))
            adj_new[b].append((a, c))
            UF.unite(a, b)

    assert UF.isSameGroup(1, N)
    flg = 0
    for v in range(1, N+1):
        if UF.isSameGroup(1, v):
            for u, _ in adj[v]:
                c = E[v * (N+1) + u]
                if c != c0 and u != N:
                    adj_new[v].append((u, c))
                    adj_new[u].append((v, c))
                    uu = u
                    flg = 1
                    break
        if flg:
            break

    # go to uu
    que = deque()
    que.append(1)
    seen = [-1] * (N + 1)
    seen[1] = 0
    par = [0] * (N + 1)
    seq = []
    while que:
        v = que.popleft()
        seq.append(v)
        for u, _ in adj_new[v]:
            if seen[u] == -1:
                seen[u] = seen[v] + 1
                par[u] = v
                que.append(u)

    v = uu
    ans = []
    color = []
    while v != 1:
        p = par[v]
        color.append(E[v * (N + 1) + p])
        ans.append(I[v * (N + 1) + p])
        v = p
    ans.reverse()
    color.reverse()

    if uu == 1:
        for u, c in adj_new[1]:
            if c != c0:
                for _ in range(2):
                    ans.append(I[u * (N+1) + 1])
                    color.append(c)
                    
    # go to N
    que = deque()
    que.append(uu)
    seen = [-1] * (N + 1)
    seen[uu] = 0
    par = [0] * (N + 1)
    seq = []
    while que:
        v = que.popleft()
        seq.append(v)
        for u, _ in adj_new[v]:
            if seen[u] == -1:
                seen[u] = seen[v] + 1
                par[u] = v
                que.append(u)

    v = N
    ans2 = []
    color2 = []
    while v != uu:
        p = par[v]
        color2.append(E[v * (N + 1) + p])
        ans2.append(I[v * (N + 1) + p])
        v = p
    ans2.reverse()
    color2.reverse()

    ans.extend(ans2)
    color.extend(color2)
    if not is_pal(color):
        print(len(ans))
        print(*ans, sep="\n")
    else:
        for _ in range(2):
            ans.append(ans[-1])
            color.append(color[-1])
        #assert is_pal(color) == 0
        print(len(ans))
        print(*ans, sep="\n")


if __name__ == '__main__':
    main()
0