結果

問題 No.1494 LCS on Tree
ユーザー mkawa2mkawa2
提出日時 2022-11-08 22:17:56
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 3,000 bytes
コンパイル時間 653 ms
コンパイル使用メモリ 87,224 KB
実行使用メモリ 849,840 KB
最終ジャッジ日時 2023-09-29 11:44:08
合計ジャッジ時間 4,965 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,268 KB
testcase_01 AC 71 ms
71,360 KB
testcase_02 AC 72 ms
71,460 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 max(self, ng=-2):
    #     if self.u1 == ng: return self.v2, self.u2
    #     return self.v1, self.u1
    #
    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):
        if val > dp[i][j]: dp[i][j] = val

    dp = [[t2() 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]:
                dp[p][i+1].update(pre.v1+1, u)
                # chmax(p, i+1, pre+1)
            elif pre:
                if p != -1: dp[p][i].update(pre.v1, u)
                if i+1 <= m: dp[u][i+1].update(pre.v1, pre.u1)

    return dp

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

ans = max(dp[0][m].v1, rdp[0][m].v1)
for u in range(n):
    for i in range(m+1):
        # pDB(u, i, dp[u][i], rdp[u][m-i])
        ans = max(ans, dp[u][i].merge(rdp[u][m-i]))
        # pDB(ans)

print(ans)
0