結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー convexineqconvexineq
提出日時 2021-03-05 22:05:39
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 375 ms / 2,000 ms
コード長 2,353 bytes
コンパイル時間 387 ms
コンパイル使用メモリ 87,136 KB
実行使用メモリ 104,300 KB
最終ジャッジ日時 2023-07-29 01:51:39
合計ジャッジ時間 10,199 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,388 KB
testcase_01 AC 73 ms
71,380 KB
testcase_02 AC 71 ms
71,536 KB
testcase_03 AC 375 ms
102,724 KB
testcase_04 AC 371 ms
102,588 KB
testcase_05 AC 359 ms
102,820 KB
testcase_06 AC 354 ms
102,988 KB
testcase_07 AC 361 ms
102,548 KB
testcase_08 AC 270 ms
94,616 KB
testcase_09 AC 178 ms
85,004 KB
testcase_10 AC 187 ms
85,632 KB
testcase_11 AC 151 ms
82,024 KB
testcase_12 AC 224 ms
90,852 KB
testcase_13 AC 255 ms
93,376 KB
testcase_14 AC 273 ms
94,384 KB
testcase_15 AC 213 ms
88,892 KB
testcase_16 AC 128 ms
79,316 KB
testcase_17 AC 133 ms
79,644 KB
testcase_18 AC 327 ms
98,080 KB
testcase_19 AC 151 ms
82,016 KB
testcase_20 AC 117 ms
78,508 KB
testcase_21 AC 211 ms
88,932 KB
testcase_22 AC 201 ms
87,624 KB
testcase_23 AC 125 ms
78,996 KB
testcase_24 AC 120 ms
78,284 KB
testcase_25 AC 131 ms
78,392 KB
testcase_26 AC 118 ms
78,340 KB
testcase_27 AC 84 ms
76,152 KB
testcase_28 AC 106 ms
77,652 KB
testcase_29 AC 126 ms
79,104 KB
testcase_30 AC 123 ms
79,172 KB
testcase_31 AC 115 ms
78,044 KB
testcase_32 AC 119 ms
78,180 KB
testcase_33 AC 139 ms
82,932 KB
testcase_34 AC 281 ms
103,288 KB
testcase_35 AC 172 ms
87,932 KB
testcase_36 AC 120 ms
79,700 KB
testcase_37 AC 263 ms
102,884 KB
testcase_38 AC 255 ms
104,300 KB
testcase_39 AC 70 ms
71,252 KB
testcase_40 AC 73 ms
71,492 KB
testcase_41 AC 72 ms
71,460 KB
testcase_42 AC 71 ms
71,352 KB
testcase_43 AC 70 ms
71,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def rerooting(g,e_M):
    """
    以下を書き換える
    """
    def merge(x,y): #値 x に y をマージ
        return (x[0]+y[0],x[1]+y[1])
    def h1(x,v): # x = dp[v][p] を親方向 v->p にマージする際の補正
        return (x[0]+1,x[1]+x[0]+2)
    #def h2(x,p,v): # x = dp[p][v] を子方向 p->v にマージする際の補正
    # 辺属性なら v が大事、頂点属性なら p が大事なことが多い
    def h2(x,p,v): return h1(x,p)
    h_fin = lambda x:x
    
    """
    dfs_bottom_up: 木DP
    dfs_top_down: rerooting
    """
    def get_order(g):
        n = len(g)
        order = []
        st = [0]
        parent = [-1]*n
        while st:
            v = st.pop()
            order.append(v)
            for c in g[v]:
                if c != parent[v]:
                    st.append(c)
                    parent[c] = v
        return order, parent

    def dfs_bottom_up(BU,hBU,parent,order):
        for i in order[::-1]:
            hBU[i] = h1(BU[i], i)
            if i: BU[parent[i]] = merge(BU[parent[i]], hBU[i])

    def dfs_top_down(hBU,hTD,g,parent,order):
        for v in order:
            acc_l = e_M
            for c in g[v]:
                if c==parent[v]:
                    acc_l = merge(acc_l,hTD[v])
                else:
                    hTD[c] = acc_l
                    acc_l = merge(acc_l,hBU[c])
            acc_r = e_M
            for c in g[v][::-1]:
                if c==parent[v]:
                    acc_r = merge(hTD[v],acc_r)
                else:
                    hTD[c] = h2(merge(hTD[c],acc_r),parent[v],v)
                    acc_r = merge(acc_r,hBU[c])
    
    order, parent = get_order(g)
    BU = [e_M]*n
    hBU = [e_M]*n
    hTD = [e_M]*n
    dfs_bottom_up(BU,hBU,parent,order)
    dfs_top_down(hBU,hTD,g,parent,order)
    
    for i in range(n):
        BU[i] = h_fin(merge(BU[i],hTD[i]))
    return BU

#######################################
# https://atcoder.jp/contests/dp/submissions/17723902
#######################################

# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

n = int(input())
g = [[] for _ in range(n)]

for _ in range(n-1):
    a,b = map(int,readline().split())
    g[a-1].append(b-1)
    g[b-1].append(a-1)

ans = n
for i,j in rerooting(g,(0,0)):
    ans += j
print(ans)
0