結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー NoneNone
提出日時 2021-10-07 14:22:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 629 ms / 2,000 ms
コード長 2,967 bytes
コンパイル時間 372 ms
コンパイル使用メモリ 86,896 KB
実行使用メモリ 121,904 KB
最終ジャッジ日時 2023-09-30 09:03:05
合計ジャッジ時間 14,846 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
71,000 KB
testcase_01 AC 76 ms
71,316 KB
testcase_02 AC 76 ms
71,236 KB
testcase_03 AC 629 ms
114,212 KB
testcase_04 AC 496 ms
113,924 KB
testcase_05 AC 523 ms
114,416 KB
testcase_06 AC 503 ms
114,272 KB
testcase_07 AC 521 ms
114,412 KB
testcase_08 AC 280 ms
93,224 KB
testcase_09 AC 184 ms
84,280 KB
testcase_10 AC 188 ms
85,412 KB
testcase_11 AC 158 ms
81,652 KB
testcase_12 AC 257 ms
89,544 KB
testcase_13 AC 273 ms
91,976 KB
testcase_14 AC 294 ms
92,736 KB
testcase_15 AC 225 ms
88,408 KB
testcase_16 AC 139 ms
79,196 KB
testcase_17 AC 138 ms
79,396 KB
testcase_18 AC 470 ms
110,844 KB
testcase_19 AC 152 ms
81,092 KB
testcase_20 AC 131 ms
78,772 KB
testcase_21 AC 227 ms
87,692 KB
testcase_22 AC 203 ms
86,548 KB
testcase_23 AC 132 ms
79,108 KB
testcase_24 AC 144 ms
79,084 KB
testcase_25 AC 129 ms
78,872 KB
testcase_26 AC 129 ms
78,904 KB
testcase_27 AC 88 ms
76,260 KB
testcase_28 AC 115 ms
77,992 KB
testcase_29 AC 140 ms
79,188 KB
testcase_30 AC 139 ms
79,108 KB
testcase_31 AC 127 ms
78,148 KB
testcase_32 AC 131 ms
78,756 KB
testcase_33 AC 195 ms
87,596 KB
testcase_34 AC 458 ms
121,904 KB
testcase_35 AC 250 ms
96,088 KB
testcase_36 AC 117 ms
80,000 KB
testcase_37 AC 238 ms
105,776 KB
testcase_38 AC 231 ms
105,064 KB
testcase_39 AC 74 ms
71,324 KB
testcase_40 AC 76 ms
71,380 KB
testcase_41 AC 75 ms
71,424 KB
testcase_42 AC 73 ms
71,344 KB
testcase_43 AC 73 ms
71,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Tree():
    def __init__(self, n, decrement=1):
        self.n = n
        self.edges = [[] for _ in range(n)]
        self.root = None
        self.size = [1]*n       # 部分木のノードの数
        self.decrement = decrement

    def add_edge(self, u, v):
        u, v = u-self.decrement, v-self.decrement
        self.edges[u].append(v)
        self.edges[v].append(u)

    def add_edges(self, edges):
        for u, v in edges:
            u, v = u-self.decrement, v-self.decrement
            self.edges[u].append(v)
            self.edges[v].append(u)

    def set_root(self, root):
        root -= self.decrement
        self.depth = [-1]*self.n
        self.root = root
        self.par = [-1]*self.n
        self.depth[root] = 0
        self.order = [root]     # 帰りがけに使う
        next_set = [root]
        while next_set:
            p = next_set.pop()
            for q in self.edges[p]:
                if self.depth[q] != -1: continue
                self.par[q] = p
                self.depth[q] = self.depth[p]+1
                self.order.append(q)
                next_set.append(q)
        for p in self.order[::-1]:
            for q in self.edges[p]:
                if self.par[p] == q: continue
                self.size[p] += self.size[q]

    def rerooting(self, op, merge, id):
        # assert self.root is not None
        dp1 = [id] * self.n
        dp2 = [id] * self.n
        for p in self.order[::-1]:
            t = id
            for q in self.edges[p]:
                if self.par[p] == q: continue
                dp2[q] = t
                t = merge(t, op(dp1[q], p, q))
            t = id
            for q in self.edges[p][::-1]:
                if self.par[p] == q: continue
                dp2[q] = merge(t, dp2[q])
                t = merge(t, op(dp1[q], p, q))
            dp1[p] = t
        for q in self.order[1:]:
            pq = self.par[q]
            dp2[q] = op(merge(dp2[q], dp2[pq]), q, pq)
            dp1[q] = merge(dp1[q], dp2[q])
        return dp1


#########################################################################################################
import sys
input = sys.stdin.readline


# edges = make_tree(10,show=True)
# N = len(edges)+1

#example()

N=int(input())
T = Tree(N)
for _ in range(N-1):
    x, y = map(int, input().split())
    T.add_edge(x,y)
T.set_root(1)


##########################################################
off = 10**12

def op(a, u, v):
    # dp を merge する前段階で実行する演算を定義(演算は頂点番号に依存させることが出来る)
    S,size=divmod(a,off)
    size+=1
    S+=size
    return S*off+size

def merge(a, b):
    # 子の op(dp,v) をどのように結合して親の dp を作るかを定義
    return a + b

# merge の単位元
id = 0
##########################################################

dp = T.rerooting(op, merge, id)
res=0
for d in dp:
    S,size=divmod(op(d,0,0),off)
    res+=S
print(res)
0