結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー minatominato
提出日時 2023-03-09 07:46:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 432 ms / 2,000 ms
コード長 470 bytes
コンパイル時間 232 ms
コンパイル使用メモリ 81,896 KB
実行使用メモリ 176,988 KB
最終ジャッジ日時 2023-10-18 06:12:42
合計ジャッジ時間 8,216 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,388 KB
testcase_01 AC 38 ms
53,388 KB
testcase_02 AC 39 ms
53,388 KB
testcase_03 AC 283 ms
87,632 KB
testcase_04 AC 289 ms
88,288 KB
testcase_05 AC 287 ms
88,064 KB
testcase_06 AC 285 ms
87,684 KB
testcase_07 AC 282 ms
87,700 KB
testcase_08 AC 216 ms
84,200 KB
testcase_09 AC 138 ms
79,812 KB
testcase_10 AC 152 ms
80,300 KB
testcase_11 AC 120 ms
78,404 KB
testcase_12 AC 191 ms
82,448 KB
testcase_13 AC 202 ms
83,448 KB
testcase_14 AC 212 ms
84,072 KB
testcase_15 AC 183 ms
82,208 KB
testcase_16 AC 95 ms
76,744 KB
testcase_17 AC 96 ms
76,860 KB
testcase_18 AC 274 ms
86,796 KB
testcase_19 AC 119 ms
78,196 KB
testcase_20 AC 90 ms
76,844 KB
testcase_21 AC 180 ms
82,144 KB
testcase_22 AC 165 ms
81,128 KB
testcase_23 AC 88 ms
76,308 KB
testcase_24 AC 92 ms
76,808 KB
testcase_25 AC 83 ms
76,228 KB
testcase_26 AC 88 ms
76,448 KB
testcase_27 AC 50 ms
61,564 KB
testcase_28 AC 58 ms
65,696 KB
testcase_29 AC 94 ms
76,552 KB
testcase_30 AC 92 ms
76,684 KB
testcase_31 AC 77 ms
73,732 KB
testcase_32 AC 86 ms
76,424 KB
testcase_33 AC 130 ms
93,304 KB
testcase_34 AC 432 ms
176,988 KB
testcase_35 AC 188 ms
109,268 KB
testcase_36 AC 85 ms
77,012 KB
testcase_37 AC 193 ms
89,712 KB
testcase_38 AC 182 ms
88,180 KB
testcase_39 AC 38 ms
53,388 KB
testcase_40 AC 38 ms
53,388 KB
testcase_41 AC 38 ms
53,388 KB
testcase_42 AC 38 ms
53,388 KB
testcase_43 AC 38 ms
53,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10**9)

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

ans = 0


def dfs(v, pv):
    global ans
    cnt = 1
    for nv in G[v]:
        if nv == pv:
            continue
        chi = dfs(nv, v)
        ans += (N - chi) * chi
        cnt += chi
    ans += cnt * (N - cnt)
    ans += N
    return cnt


dfs(0, -1)
print(ans)
0