結果

問題 No.1494 LCS on Tree
ユーザー mkawa2mkawa2
提出日時 2022-11-09 15:41:39
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,539 bytes
コンパイル時間 1,031 ms
コンパイル使用メモリ 87,228 KB
実行使用メモリ 143,584 KB
最終ジャッジ日時 2023-09-30 03:43:45
合計ジャッジ時間 23,547 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
71,240 KB
testcase_01 AC 68 ms
71,312 KB
testcase_02 AC 63 ms
71,112 KB
testcase_03 AC 756 ms
141,836 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 702 ms
142,088 KB
testcase_15 AC 691 ms
141,832 KB
testcase_16 AC 741 ms
141,924 KB
testcase_17 AC 732 ms
142,068 KB
testcase_18 AC 708 ms
142,080 KB
testcase_19 AC 70 ms
71,232 KB
testcase_20 AC 63 ms
71,144 KB
testcase_21 AC 63 ms
71,148 KB
testcase_22 AC 63 ms
71,380 KB
testcase_23 AC 67 ms
71,220 KB
testcase_24 AC 67 ms
71,320 KB
testcase_25 AC 66 ms
71,312 KB
testcase_26 AC 69 ms
71,116 KB
testcase_27 AC 67 ms
71,388 KB
testcase_28 AC 68 ms
71,388 KB
testcase_29 AC 64 ms
71,296 KB
testcase_30 AC 724 ms
142,452 KB
testcase_31 AC 716 ms
142,484 KB
testcase_32 AC 722 ms
141,788 KB
testcase_33 AC 733 ms
141,784 KB
testcase_34 AC 721 ms
141,700 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 AC 475 ms
141,560 KB
testcase_46 AC 482 ms
141,644 KB
testcase_47 AC 500 ms
141,536 KB
testcase_48 AC 523 ms
141,540 KB
testcase_49 AC 506 ms
141,640 KB
権限があれば一括ダウンロードができます

ソースコード

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

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 = [[0]*(m+1) for _ in range(n)]
    for u in uu[::-1]:
        if u == 0: break
        if len(to[u]) == 1:
            for i, c in enumerate(s):
                if c == cc[u]:
                    dp[u][i+1] = 1
                    break
        p = parent[u]
        for i in range(m+1):
            pre = dp[u][i]
            if i < m and cc[p] == s[i]:
                chmax(p, i+1, pre+1)
            elif pre:
                chmax(p, i, pre)
                if i+1 <= m: chmax(u, i+1, pre)

    return dp

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

ans = 0
for u in range(n):
    for i in range(m+1):
        m1 = m2 = 0
        for v, _ in to[u]:
            if v == parent[u]: continue
            v1, v2 = dp[v][i], rdp[v][m-i]
            if v1+m2 > ans: ans = v1+m2
            if v2+m1 > ans: ans = v2+m1
            if v1 > m1: m1 = v1
            if v2 > m2: m2 = v2

print(ans)
0