結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2021-06-19 16:19:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 427 ms / 2,000 ms
コード長 627 bytes
コンパイル時間 488 ms
コンパイル使用メモリ 87,092 KB
実行使用メモリ 182,180 KB
最終ジャッジ日時 2023-09-05 00:58:22
合計ジャッジ時間 11,651 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,460 KB
testcase_01 AC 72 ms
71,352 KB
testcase_02 AC 71 ms
71,096 KB
testcase_03 AC 303 ms
91,572 KB
testcase_04 AC 309 ms
92,456 KB
testcase_05 AC 314 ms
91,988 KB
testcase_06 AC 309 ms
92,108 KB
testcase_07 AC 313 ms
91,616 KB
testcase_08 AC 251 ms
87,436 KB
testcase_09 AC 173 ms
83,224 KB
testcase_10 AC 187 ms
84,328 KB
testcase_11 AC 154 ms
81,780 KB
testcase_12 AC 225 ms
86,548 KB
testcase_13 AC 237 ms
87,264 KB
testcase_14 AC 242 ms
87,856 KB
testcase_15 AC 217 ms
87,360 KB
testcase_16 AC 128 ms
78,380 KB
testcase_17 AC 130 ms
78,416 KB
testcase_18 AC 327 ms
90,256 KB
testcase_19 AC 148 ms
80,920 KB
testcase_20 AC 125 ms
78,268 KB
testcase_21 AC 202 ms
85,252 KB
testcase_22 AC 199 ms
84,896 KB
testcase_23 AC 121 ms
78,104 KB
testcase_24 AC 123 ms
78,428 KB
testcase_25 AC 119 ms
78,784 KB
testcase_26 AC 122 ms
78,148 KB
testcase_27 AC 86 ms
75,956 KB
testcase_28 AC 98 ms
76,264 KB
testcase_29 AC 128 ms
78,364 KB
testcase_30 AC 127 ms
78,388 KB
testcase_31 AC 117 ms
78,632 KB
testcase_32 AC 122 ms
78,184 KB
testcase_33 AC 167 ms
97,648 KB
testcase_34 AC 427 ms
182,180 KB
testcase_35 AC 214 ms
112,616 KB
testcase_36 AC 117 ms
78,608 KB
testcase_37 AC 218 ms
92,096 KB
testcase_38 AC 209 ms
90,548 KB
testcase_39 AC 72 ms
71,096 KB
testcase_40 AC 70 ms
71,324 KB
testcase_41 AC 72 ms
71,404 KB
testcase_42 AC 71 ms
71,504 KB
testcase_43 AC 71 ms
71,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(2*10**5)
n = int(input())
e = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    e[a-1].append(b-1)
    e[b-1].append(a-1)

size = [0]*n
def dfs(x,p=-1):
    s = 0
    for nex in e[x]:
        if nex == p:
            continue
        s += dfs(nex,x)
    s += 1
    size[x] = s
    return s

dfs(0)
ans = n*n
for i in range(n):
    p = -1
    c = 0
    for nex in e[i]:
        if size[nex] > size[i]:
            p = nex
        else:
            ans += size[nex]*(n-size[nex])
            c += size[nex]
    if p != -1:
        ans += (n-c-1)*(c+1) 
print(ans)
0