結果

問題 No.1494 LCS on Tree
ユーザー 草苺奶昔草苺奶昔
提出日時 2023-03-14 10:23:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 767 ms / 2,000 ms
コード長 3,483 bytes
コンパイル時間 313 ms
コンパイル使用メモリ 82,352 KB
実行使用メモリ 295,680 KB
最終ジャッジ日時 2024-11-20 15:10:59
合計ジャッジ時間 21,705 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
67,704 KB
testcase_01 AC 61 ms
68,156 KB
testcase_02 AC 59 ms
68,220 KB
testcase_03 AC 607 ms
268,804 KB
testcase_04 AC 629 ms
269,896 KB
testcase_05 AC 610 ms
269,532 KB
testcase_06 AC 623 ms
270,232 KB
testcase_07 AC 566 ms
269,440 KB
testcase_08 AC 568 ms
269,924 KB
testcase_09 AC 561 ms
259,748 KB
testcase_10 AC 569 ms
269,456 KB
testcase_11 AC 635 ms
259,432 KB
testcase_12 AC 635 ms
270,008 KB
testcase_13 AC 573 ms
269,296 KB
testcase_14 AC 645 ms
268,056 KB
testcase_15 AC 598 ms
269,384 KB
testcase_16 AC 654 ms
269,772 KB
testcase_17 AC 648 ms
269,132 KB
testcase_18 AC 602 ms
269,604 KB
testcase_19 AC 63 ms
69,148 KB
testcase_20 AC 61 ms
68,300 KB
testcase_21 AC 63 ms
68,980 KB
testcase_22 AC 62 ms
68,104 KB
testcase_23 AC 63 ms
69,184 KB
testcase_24 AC 63 ms
68,440 KB
testcase_25 AC 62 ms
67,888 KB
testcase_26 AC 62 ms
67,972 KB
testcase_27 AC 61 ms
69,000 KB
testcase_28 AC 61 ms
68,512 KB
testcase_29 AC 61 ms
67,880 KB
testcase_30 AC 612 ms
269,588 KB
testcase_31 AC 612 ms
269,684 KB
testcase_32 AC 660 ms
259,532 KB
testcase_33 AC 664 ms
269,144 KB
testcase_34 AC 618 ms
269,712 KB
testcase_35 AC 113 ms
85,028 KB
testcase_36 AC 303 ms
125,484 KB
testcase_37 AC 101 ms
81,896 KB
testcase_38 AC 202 ms
118,712 KB
testcase_39 AC 352 ms
161,444 KB
testcase_40 AC 158 ms
94,764 KB
testcase_41 AC 509 ms
215,812 KB
testcase_42 AC 147 ms
94,564 KB
testcase_43 AC 444 ms
190,576 KB
testcase_44 AC 364 ms
159,596 KB
testcase_45 AC 767 ms
295,628 KB
testcase_46 AC 704 ms
295,388 KB
testcase_47 AC 700 ms
295,648 KB
testcase_48 AC 736 ms
295,644 KB
testcase_49 AC 646 ms
295,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
from typing import List, Tuple
from typing import Callable, Generic, List, TypeVar

T = TypeVar("T")


class Rerooting(Generic[T]):

    __slots__ = ("adjList", "_n", "_decrement")

    def __init__(self, n: int, decrement: int = 0):
        self.adjList = [[] for _ in range(n)]
        self._n = n
        self._decrement = decrement

    def addEdge(self, u: int, v: int) -> None:
        u -= self._decrement
        v -= self._decrement
        self.adjList[u].append(v)
        self.adjList[v].append(u)

    def rerooting(
        self,
        e: Callable[[int], T],
        op: Callable[[T, T], T],
        composition: Callable[[T, int, int, int], T],
        root=0,
    ) -> List["T"]:
        root -= self._decrement
        assert 0 <= root < self._n
        parents = [-1] * self._n
        order = [root]
        stack = [root]
        while stack:
            cur = stack.pop()
            for next in self.adjList[cur]:
                if next == parents[cur]:
                    continue
                parents[next] = cur
                order.append(next)
                stack.append(next)

        dp1 = [e(i) for i in range(self._n)]
        dp2 = [e(i) for i in range(self._n)]
        for cur in order[::-1]:
            res = e(cur)
            for next in self.adjList[cur]:
                if parents[cur] == next:
                    continue
                dp2[next] = res
                res = op(res, composition(dp1[next], cur, next, 0))
            res = e(cur)
            for next in self.adjList[cur][::-1]:
                if parents[cur] == next:
                    continue
                dp2[next] = op(res, dp2[next])
                res = op(res, composition(dp1[next], cur, next, 0))
            dp1[cur] = res

        for newRoot in order[1:]:
            parent = parents[newRoot]
            dp2[newRoot] = composition(op(dp2[newRoot], dp2[parent]), parent, newRoot, 1)
            dp1[newRoot] = op(dp1[newRoot], dp2[newRoot])
        return dp1





def lcsOnTree(n: int, edges: List[Tuple[int, int, str]], target: str) -> int:
    def e(root: int) -> List[int]:
        return [0] * (len(target) + 1)

    def op(childRes1: List[int], childRes2: List[int]) -> List[int]:
        res = childRes1[:]
        for i in range(len(res)):
            if res[i] < childRes2[i]:
                res[i] = childRes2[i]
        return res

    def composition(fromRes: List[int], parent: int, cur: int, direction: int) -> List[int]:
        """direction: 0: cur -> parent, 1: parent -> cur"""
        from_, to = (cur, parent) if direction == 0 else (parent, cur)
        c = weights[from_][to]
        res = fromRes[:]
        for i in range(len(s) - 1, -1, -1):
            if s[i] == c:
                if res[i + 1] < res[i] + 1:
                    res[i + 1] = res[i] + 1
        for i in range(len(s)):
            if res[i + 1] < res[i]:
                res[i + 1] = res[i]
        return res

    R = Rerooting(n)
    weights = [defaultdict(str) for _ in range(n)]
    for u, v, c in edges:
        R.addEdge(u, v)
        weights[u][v] = c
        weights[v][u] = c

    dp = R.rerooting(e=e, op=op, composition=composition, root=0)
    return max(v[-1] for v in dp)
if __name__ == "__main__":
    n = int(input())
    s = input()
    edges = []
    for _ in range(n - 1):
        u, v, c = input().split()
        edges.append((int(u) - 1, int(v) - 1, c))

    print(lcsOnTree(n, edges, s))
0