結果

問題 No.1494 LCS on Tree
ユーザー mkawa2mkawa2
提出日時 2022-11-08 22:29:59
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 3,167 bytes
コンパイル時間 182 ms
コンパイル使用メモリ 82,128 KB
実行使用メモリ 849,224 KB
最終ジャッジ日時 2024-07-22 06:18:36
合計ジャッジ時間 3,737 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,992 KB
testcase_01 AC 42 ms
52,992 KB
testcase_02 AC 38 ms
52,736 KB
testcase_03 MLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
pDB = 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 << 63)-1
# inf = (1 << 31)-1
# md = 10**9+7
md = 998244353

# class t2:
#     def __init__(self):
#         self.v1 = 0
#         self.u1 = -1
#         self.v2 = -1
#         self.u2 = -1
#
#     def __repr__(self):
#         return f"({self.v1},{self.u1},{self.v2},{self.u2})"
#
#     def update(self, v, u):
#         if v > self.v1:
#             self.v2, self.u2 = self.v1, self.u1
#             self.v1, self.u1 = v, u
#         elif v > self.v2 and u != self.u1:
#             self.v2, self.u2 = v, u
#
#     def merge(self, other):
#         if self.u1 == other.u1:
#             return max(self.v1+other.v2, self.v2+other.v1)
#         return other.v1+self.v1

n = II()
s = [ord(c)-97 for c in SI()]
m = len(s)
to = [[] for _ in range(n)]
for _ in range(n-1):
    u, v, c = SI().split()
    u = int1(u)
    v = int1(v)
    c = ord(c)-97
    to[u].append((v, c))
    to[v].append((u, c))

parent, depth, vis = [-1]*n, [0]*n, [0]*n
cc = [""]*n

def dfs(root):
    uu = []
    vis[root] = 1
    stack = [root]
    while stack:
        iu = stack.pop()
        i, u = divmod(iu, n)
        if i == 0:
            uu.append(u)
        # care to's format
        while i < len(to[u]) and vis[to[u][i][0]]: i += 1
        if i < len(to[u]):
            stack.append((i+1)*n+u)
            # care to's format
            v, c = to[u][i]
            vis[v] = 1
            stack.append(v)
            parent[v] = u
            depth[v] = depth[u]+1
            cc[v] = c

    return uu

uu = dfs(0)

def dodp(s):
    def chmax(i, j, val, u):
        if val > dp[i][j][0]:
            dp[i][j][0], dp[i][j][1] = val, u
            return
        if val > dp[i][j][2] and u != dp[i][j][1]:
            dp[i][j][2], dp[i][j][3] = val, u
            return

    dp = [[[0, -1, -1, -1] for _ in range(m+1)] for _ in range(n)]
    for u in uu[::-1]:
        p = parent[u]
        for i, c in enumerate(s):
            pre = dp[u][i]
            if c == cc[u]:
                chmax(p, i+1, pre[0]+1, u)
            elif pre[0]:
                if p != -1: chmax(p, i, pre[0], u)
                if i+1 <= m: chmax(u, i+1, pre[0], pre[1])

    return dp

dp = dodp(s)
rdp = dodp(s[::-1])

ans = max(dp[0][m][0], rdp[0][m][0])
for u in range(n):
    for i in range(m+1):
        a, b, c, d = dp[u][i]
        p, q, r, s = rdp[u][m-i]
        if b == q:
            ans = max(ans, a+r, c+p)
        else:
            ans = max(ans, a+p)
        # pDB(u, i, dp[u][i], rdp[u][m-i])
        # pDB(ans)

print(ans)
0