結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー tktk_snsntktk_snsn
提出日時 2021-03-15 20:47:25
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 488 ms / 2,000 ms
コード長 674 bytes
コンパイル時間 153 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 30,592 KB
最終ジャッジ日時 2024-04-25 00:08:28
合計ジャッジ時間 9,814 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
10,752 KB
testcase_01 AC 25 ms
10,752 KB
testcase_02 AC 28 ms
10,624 KB
testcase_03 AC 471 ms
30,336 KB
testcase_04 AC 488 ms
30,592 KB
testcase_05 AC 467 ms
30,592 KB
testcase_06 AC 473 ms
30,336 KB
testcase_07 AC 472 ms
30,592 KB
testcase_08 AC 315 ms
23,936 KB
testcase_09 AC 147 ms
17,152 KB
testcase_10 AC 156 ms
17,664 KB
testcase_11 AC 93 ms
14,464 KB
testcase_12 AC 242 ms
21,248 KB
testcase_13 AC 302 ms
23,040 KB
testcase_14 AC 303 ms
23,680 KB
testcase_15 AC 225 ms
20,480 KB
testcase_16 AC 47 ms
11,904 KB
testcase_17 AC 49 ms
11,904 KB
testcase_18 AC 433 ms
28,672 KB
testcase_19 AC 88 ms
14,208 KB
testcase_20 AC 37 ms
11,264 KB
testcase_21 AC 215 ms
20,224 KB
testcase_22 AC 182 ms
18,688 KB
testcase_23 AC 38 ms
11,392 KB
testcase_24 AC 41 ms
11,392 KB
testcase_25 AC 35 ms
11,264 KB
testcase_26 AC 38 ms
11,264 KB
testcase_27 AC 30 ms
10,752 KB
testcase_28 AC 29 ms
10,880 KB
testcase_29 AC 42 ms
11,648 KB
testcase_30 AC 42 ms
11,392 KB
testcase_31 AC 35 ms
11,008 KB
testcase_32 AC 37 ms
11,264 KB
testcase_33 AC 107 ms
14,976 KB
testcase_34 AC 456 ms
29,952 KB
testcase_35 AC 186 ms
18,944 KB
testcase_36 AC 68 ms
12,928 KB
testcase_37 AC 362 ms
26,928 KB
testcase_38 AC 328 ms
25,664 KB
testcase_39 AC 27 ms
10,624 KB
testcase_40 AC 26 ms
10,624 KB
testcase_41 AC 27 ms
10,624 KB
testcase_42 AC 27 ms
10,624 KB
testcase_43 AC 27 ms
10,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

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

topo = []
par = [-1] * N
q = [0]
while q:
    s = q.pop()
    topo.append(s)
    for t in edge[s]:
        if t == par[s]:
            continue
        par[t] = s
        q.append(t)
        edge[t].remove(s)


ans = N * N
size = [1] * N
for s in topo[::-1]:
    tmp = N - 1
    for c in edge[s]:
        ans += (N - size[c]) * size[c]
        tmp -= size[c]
    ans += size[s] * tmp
    size[par[s]] += size[s]

print(ans)
0