結果

問題 No.1494 LCS on Tree
ユーザー 草苺奶昔草苺奶昔
提出日時 2023-03-14 10:07:43
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,411 bytes
コンパイル時間 202 ms
コンパイル使用メモリ 81,704 KB
実行使用メモリ 175,240 KB
最終ジャッジ日時 2023-10-18 11:30:38
合計ジャッジ時間 19,426 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
68,060 KB
testcase_01 WA -
testcase_02 AC 63 ms
68,060 KB
testcase_03 WA -
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 591 ms
173,656 KB
testcase_15 AC 492 ms
173,284 KB
testcase_16 AC 511 ms
171,700 KB
testcase_17 AC 506 ms
172,784 KB
testcase_18 AC 498 ms
171,740 KB
testcase_19 WA -
testcase_20 AC 63 ms
68,164 KB
testcase_21 WA -
testcase_22 AC 64 ms
68,164 KB
testcase_23 AC 64 ms
68,164 KB
testcase_24 WA -
testcase_25 AC 63 ms
68,164 KB
testcase_26 WA -
testcase_27 AC 64 ms
68,164 KB
testcase_28 WA -
testcase_29 AC 62 ms
68,164 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
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 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
権限があれば一括ダウンロードができます

ソースコード

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]:
        for i in range(len(childRes1)):
            childRes1[i] = max(childRes1[i], childRes2[i])
        return childRes1

    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]
        for i in range(len(s) - 1, -1, -1):
            if s[i] == c:
                fromRes[i + 1] = max(fromRes[i + 1], fromRes[i] + 1)
        for i in range(len(s)):
            fromRes[i + 1] = max(fromRes[i + 1], fromRes[i])
        return fromRes

    R = Rerooting[List[int]](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