結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー H3PO4H3PO4
提出日時 2022-05-09 13:32:37
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 436 ms / 2,000 ms
コード長 609 bytes
コンパイル時間 100 ms
コンパイル使用メモリ 10,884 KB
実行使用メモリ 35,664 KB
最終ジャッジ日時 2023-09-23 12:20:03
合計ジャッジ時間 9,865 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,576 KB
testcase_01 AC 18 ms
8,456 KB
testcase_02 AC 18 ms
8,624 KB
testcase_03 AC 423 ms
33,044 KB
testcase_04 AC 431 ms
33,136 KB
testcase_05 AC 425 ms
33,092 KB
testcase_06 AC 411 ms
33,032 KB
testcase_07 AC 436 ms
33,036 KB
testcase_08 AC 287 ms
24,964 KB
testcase_09 AC 131 ms
16,060 KB
testcase_10 AC 140 ms
16,880 KB
testcase_11 AC 84 ms
13,248 KB
testcase_12 AC 218 ms
21,564 KB
testcase_13 AC 258 ms
24,008 KB
testcase_14 AC 266 ms
24,680 KB
testcase_15 AC 201 ms
20,600 KB
testcase_16 AC 37 ms
10,040 KB
testcase_17 AC 38 ms
10,064 KB
testcase_18 AC 387 ms
30,716 KB
testcase_19 AC 77 ms
12,760 KB
testcase_20 AC 28 ms
9,276 KB
testcase_21 AC 211 ms
20,116 KB
testcase_22 AC 170 ms
18,584 KB
testcase_23 AC 30 ms
9,420 KB
testcase_24 AC 30 ms
9,392 KB
testcase_25 AC 27 ms
9,008 KB
testcase_26 AC 29 ms
9,392 KB
testcase_27 AC 20 ms
8,588 KB
testcase_28 AC 21 ms
8,632 KB
testcase_29 AC 34 ms
9,668 KB
testcase_30 AC 31 ms
9,480 KB
testcase_31 AC 25 ms
9,064 KB
testcase_32 AC 28 ms
9,264 KB
testcase_33 AC 90 ms
14,660 KB
testcase_34 AC 387 ms
35,644 KB
testcase_35 AC 162 ms
19,752 KB
testcase_36 AC 56 ms
11,976 KB
testcase_37 AC 326 ms
35,664 KB
testcase_38 AC 302 ms
33,496 KB
testcase_39 AC 18 ms
8,588 KB
testcase_40 AC 19 ms
8,612 KB
testcase_41 AC 19 ms
8,452 KB
testcase_42 AC 19 ms
8,548 KB
testcase_43 AC 19 ms
8,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

input = sys.stdin.buffer.readline

N = int(input())
T = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = (int(x) - 1 for x in input().split())
    T[a].append(b)
    T[b].append(a)

d = deque([(0, -1)])
dfs_order = []

while d:
    v, p = d.pop()
    for x in T[v]:
        if x == p:
            continue
        dfs_order.append((x, v))
        d.append((x, v))

subtree_size = [1] * N

for x, v in reversed(dfs_order):
    subtree_size[v] += subtree_size[x]

ans = N * N
for v in range(N):
    ans += subtree_size[v] * (N - subtree_size[v]) * 2
print(ans)
0