結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー brthyyjpbrthyyjp
提出日時 2021-03-05 21:50:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 232 ms / 2,000 ms
コード長 824 bytes
コンパイル時間 165 ms
コンパイル使用メモリ 82,944 KB
実行使用メモリ 105,604 KB
最終ジャッジ日時 2024-04-16 09:16:42
合計ジャッジ時間 6,546 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
52,096 KB
testcase_01 AC 42 ms
52,352 KB
testcase_02 AC 41 ms
51,968 KB
testcase_03 AC 232 ms
96,548 KB
testcase_04 AC 228 ms
96,596 KB
testcase_05 AC 226 ms
96,672 KB
testcase_06 AC 225 ms
96,180 KB
testcase_07 AC 226 ms
96,168 KB
testcase_08 AC 163 ms
87,548 KB
testcase_09 AC 115 ms
82,176 KB
testcase_10 AC 117 ms
82,560 KB
testcase_11 AC 104 ms
79,360 KB
testcase_12 AC 145 ms
85,824 KB
testcase_13 AC 155 ms
86,904 KB
testcase_14 AC 163 ms
87,460 KB
testcase_15 AC 140 ms
84,800 KB
testcase_16 AC 86 ms
73,728 KB
testcase_17 AC 86 ms
73,984 KB
testcase_18 AC 209 ms
94,244 KB
testcase_19 AC 102 ms
78,592 KB
testcase_20 AC 83 ms
71,680 KB
testcase_21 AC 135 ms
84,564 KB
testcase_22 AC 130 ms
83,816 KB
testcase_23 AC 82 ms
71,040 KB
testcase_24 AC 79 ms
70,144 KB
testcase_25 AC 84 ms
72,704 KB
testcase_26 AC 79 ms
70,144 KB
testcase_27 AC 52 ms
59,776 KB
testcase_28 AC 68 ms
66,176 KB
testcase_29 AC 87 ms
73,728 KB
testcase_30 AC 85 ms
72,832 KB
testcase_31 AC 76 ms
68,608 KB
testcase_32 AC 85 ms
72,576 KB
testcase_33 AC 104 ms
80,000 KB
testcase_34 AC 189 ms
93,028 KB
testcase_35 AC 115 ms
83,916 KB
testcase_36 AC 77 ms
72,832 KB
testcase_37 AC 172 ms
105,604 KB
testcase_38 AC 152 ms
99,416 KB
testcase_39 AC 41 ms
52,224 KB
testcase_40 AC 41 ms
51,840 KB
testcase_41 AC 41 ms
52,224 KB
testcase_42 AC 42 ms
51,968 KB
testcase_43 AC 42 ms
52,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

n = int(input())
g = [[] for i in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    g[a].append(b)
    g[b].append(a)

s = []
s.append(0)
parent = [-1]*n
order = []
while s:
    v = s.pop()
    order.append(v)
    for u in g[v]:
        if u == parent[v]:
            continue
        s.append(u)
        parent[u] = v
order.reverse()
dp = [1]*n
for v in order:
    if parent[v] != -1:
        dp[parent[v]] += dp[v]
#print(dp)

ans = 0
for v in range(n):
    for u in g[v]:
        if u == parent[v]:
            t = dp[v]*(n-dp[v])
            ans += dp[v]*(n-dp[v])
        else:
            t = (n-dp[u])*dp[u]
            ans += (n-dp[u])*dp[u]
        #print(v, u, t)
ans += n*n
print(ans)
0