結果

問題 No.1718 Random Squirrel
ユーザー 草苺奶昔草苺奶昔
提出日時 2023-03-26 20:42:30
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 3,239 bytes
コンパイル時間 272 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 69,504 KB
最終ジャッジ日時 2024-09-19 09:59:13
合計ジャッジ時間 4,913 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
from typing import List, Tuple
from Rerooting import Rerooting

INF = int(1e18)


def randomSquirrel(n: int, edges: List[Tuple[int, int]], sweets: List[int]) -> List[int]:
    E = Tuple[int, int]  # (maxDist, mustVisitCount)

    def e(root: int) -> E:
        return (0, 0) if isSpecial[root] else (-INF, 0)

    def op(childRes1: E, childRes2: E) -> E:
        dist1, must1 = childRes1
        dist2, must2 = childRes2
        return (max(dist1, dist2), must1 + must2)

    def composition(fromRes: E, parent: int, cur: int, direction: int) -> E:
        """direction: 0: cur -> parent, 1: parent -> cur"""
        dist, must = fromRes
        w = weights[cur][parent] if direction == 0 else weights[parent][cur]
        return (dist + w, must + 1) if dist >= 0 else (dist, must)

    isSpecial = [False] * n
    for v in sweets:
        isSpecial[v] = True

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

    dp = R.rerooting(e=e, op=op, composition=composition, root=0)
    res = [mustVisit * 2 - maxDist for maxDist, mustVisit in dp]  # 可以不回到出发点,2*点数-最大距离
    return res



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
0