結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2021-03-05 22:05:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 478 ms / 2,000 ms
コード長 747 bytes
コンパイル時間 174 ms
コンパイル使用メモリ 82,492 KB
実行使用メモリ 220,672 KB
最終ジャッジ日時 2024-04-16 09:34:34
合計ジャッジ時間 8,193 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,096 KB
testcase_01 AC 41 ms
51,840 KB
testcase_02 AC 41 ms
51,968 KB
testcase_03 AC 284 ms
88,704 KB
testcase_04 AC 290 ms
89,204 KB
testcase_05 AC 282 ms
88,960 KB
testcase_06 AC 291 ms
89,128 KB
testcase_07 AC 289 ms
89,216 KB
testcase_08 AC 224 ms
84,736 KB
testcase_09 AC 144 ms
80,512 KB
testcase_10 AC 158 ms
80,848 KB
testcase_11 AC 117 ms
79,104 KB
testcase_12 AC 195 ms
83,328 KB
testcase_13 AC 227 ms
85,760 KB
testcase_14 AC 219 ms
85,604 KB
testcase_15 AC 188 ms
82,304 KB
testcase_16 AC 111 ms
77,056 KB
testcase_17 AC 106 ms
77,712 KB
testcase_18 AC 281 ms
88,576 KB
testcase_19 AC 112 ms
78,592 KB
testcase_20 AC 88 ms
77,056 KB
testcase_21 AC 177 ms
82,560 KB
testcase_22 AC 166 ms
82,176 KB
testcase_23 AC 92 ms
77,056 KB
testcase_24 AC 96 ms
77,184 KB
testcase_25 AC 78 ms
73,600 KB
testcase_26 AC 90 ms
77,440 KB
testcase_27 AC 48 ms
60,160 KB
testcase_28 AC 58 ms
64,000 KB
testcase_29 AC 98 ms
77,184 KB
testcase_30 AC 95 ms
77,184 KB
testcase_31 AC 76 ms
73,216 KB
testcase_32 AC 90 ms
76,800 KB
testcase_33 AC 127 ms
97,280 KB
testcase_34 AC 478 ms
220,672 KB
testcase_35 AC 231 ms
139,392 KB
testcase_36 AC 77 ms
77,556 KB
testcase_37 AC 173 ms
92,932 KB
testcase_38 AC 165 ms
90,880 KB
testcase_39 AC 41 ms
51,840 KB
testcase_40 AC 41 ms
51,840 KB
testcase_41 AC 41 ms
52,096 KB
testcase_42 AC 40 ms
52,480 KB
testcase_43 AC 40 ms
52,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""

yに関してxがどちら側にいるかだけで変化する
yのサイズをYとする

x == y の時 N
xがyの子の時、(N-Y+1)
xがyの親の時、Y

"""

import sys
from sys import stdin
sys.setrecursionlimit(200000)

ans = 0

def dfs(v,p):

    global ans
    tmp = []
    for nex in lis[v]:
        if nex != p:
            tmp.append ( dfs(nex,v) )

    for x in tmp:
        ans += (N-x) * x

    ans += N
    chnum[v] += sum(tmp)
    rem = N - chnum[v]
    ans += (N-rem) * rem
    
    return chnum[v]
    

N = int(input())

lis = [ [] for i in range(N) ]

for i in range(N-1):

    A,B = map(int,stdin.readline().split())
    A -= 1
    B -= 1

    lis[A].append(B)
    lis[B].append(A)

chnum = [1] * N
dfs(0,0)
print (ans)
0