結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー 草苺奶昔草苺奶昔
提出日時 2023-03-13 19:39:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 509 ms / 2,000 ms
コード長 2,802 bytes
コンパイル時間 318 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 179,488 KB
最終ジャッジ日時 2023-10-18 11:15:14
合計ジャッジ時間 11,976 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
68,140 KB
testcase_01 AC 65 ms
68,140 KB
testcase_02 AC 64 ms
68,140 KB
testcase_03 AC 456 ms
100,724 KB
testcase_04 AC 471 ms
100,848 KB
testcase_05 AC 457 ms
100,692 KB
testcase_06 AC 455 ms
100,976 KB
testcase_07 AC 461 ms
100,848 KB
testcase_08 AC 348 ms
93,900 KB
testcase_09 AC 232 ms
84,916 KB
testcase_10 AC 239 ms
86,664 KB
testcase_11 AC 196 ms
82,248 KB
testcase_12 AC 315 ms
90,920 KB
testcase_13 AC 325 ms
91,364 KB
testcase_14 AC 345 ms
93,072 KB
testcase_15 AC 301 ms
89,424 KB
testcase_16 AC 166 ms
80,616 KB
testcase_17 AC 169 ms
80,524 KB
testcase_18 AC 448 ms
96,864 KB
testcase_19 AC 199 ms
81,992 KB
testcase_20 AC 160 ms
79,856 KB
testcase_21 AC 290 ms
88,412 KB
testcase_22 AC 269 ms
88,132 KB
testcase_23 AC 150 ms
79,784 KB
testcase_24 AC 159 ms
79,796 KB
testcase_25 AC 149 ms
79,712 KB
testcase_26 AC 154 ms
79,824 KB
testcase_27 AC 91 ms
77,608 KB
testcase_28 AC 120 ms
78,636 KB
testcase_29 AC 166 ms
80,612 KB
testcase_30 AC 160 ms
79,960 KB
testcase_31 AC 145 ms
79,716 KB
testcase_32 AC 149 ms
79,748 KB
testcase_33 AC 217 ms
100,988 KB
testcase_34 AC 509 ms
179,488 KB
testcase_35 AC 298 ms
125,316 KB
testcase_36 AC 141 ms
80,380 KB
testcase_37 AC 295 ms
104,428 KB
testcase_38 AC 278 ms
104,024 KB
testcase_39 AC 65 ms
68,140 KB
testcase_40 AC 66 ms
68,140 KB
testcase_41 AC 65 ms
68,140 KB
testcase_42 AC 65 ms
68,140 KB
testcase_43 AC 65 ms
68,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys


sys.setrecursionlimit(int(1e9))

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 e(root: int) -> int:
    return 0

def op(childRes1: int, childRes2: int) -> int:
    return childRes1 + childRes2

def composition(fromRes: int, parent: int, cur: int, direction: int) -> int:
    if direction == 0:  # cur -> parent
        return fromRes + subSize[cur]
    return fromRes + (n - subSize[cur])  # parent -> cur

def dfsForSubSize(cur: int, parent: int) -> int:
    res = 1
    for next in R.adjList[cur]:
        if next != parent:
            res += dfsForSubSize(next, cur)
    subSize[cur] = res
    return res

n = int(input())
R = Rerooting(n)
for _ in range(n - 1):
    u, v = map(int, input().split())
    R.addEdge(u - 1, v - 1)

subSize = [0] * n
dfsForSubSize(0, -1)
dp = R.rerooting(e=e, op=op, composition=composition, root=0)
print(sum(dp) + n * n)
0