結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー persimmon-persimmonpersimmon-persimmon
提出日時 2021-03-11 14:19:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 299 ms / 2,000 ms
コード長 597 bytes
コンパイル時間 172 ms
コンパイル使用メモリ 82,508 KB
実行使用メモリ 112,872 KB
最終ジャッジ日時 2024-04-21 04:22:33
合計ジャッジ時間 8,618 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
53,080 KB
testcase_01 AC 44 ms
53,328 KB
testcase_02 AC 38 ms
52,944 KB
testcase_03 AC 299 ms
105,608 KB
testcase_04 AC 289 ms
105,956 KB
testcase_05 AC 289 ms
105,768 KB
testcase_06 AC 286 ms
105,708 KB
testcase_07 AC 284 ms
105,696 KB
testcase_08 AC 213 ms
97,060 KB
testcase_09 AC 156 ms
85,524 KB
testcase_10 AC 157 ms
86,784 KB
testcase_11 AC 137 ms
81,684 KB
testcase_12 AC 198 ms
92,416 KB
testcase_13 AC 203 ms
94,736 KB
testcase_14 AC 218 ms
95,196 KB
testcase_15 AC 186 ms
90,060 KB
testcase_16 AC 106 ms
77,432 KB
testcase_17 AC 103 ms
77,728 KB
testcase_18 AC 261 ms
101,164 KB
testcase_19 AC 126 ms
80,724 KB
testcase_20 AC 98 ms
77,220 KB
testcase_21 AC 185 ms
90,548 KB
testcase_22 AC 158 ms
86,192 KB
testcase_23 AC 105 ms
77,296 KB
testcase_24 AC 104 ms
77,788 KB
testcase_25 AC 101 ms
77,556 KB
testcase_26 AC 102 ms
77,568 KB
testcase_27 AC 55 ms
62,052 KB
testcase_28 AC 80 ms
71,048 KB
testcase_29 AC 112 ms
77,528 KB
testcase_30 AC 112 ms
77,752 KB
testcase_31 AC 95 ms
77,240 KB
testcase_32 AC 100 ms
77,436 KB
testcase_33 AC 124 ms
81,940 KB
testcase_34 AC 228 ms
103,636 KB
testcase_35 AC 147 ms
86,072 KB
testcase_36 AC 100 ms
77,252 KB
testcase_37 AC 245 ms
112,872 KB
testcase_38 AC 252 ms
110,912 KB
testcase_39 AC 46 ms
52,896 KB
testcase_40 AC 45 ms
53,300 KB
testcase_41 AC 42 ms
53,208 KB
testcase_42 AC 40 ms
52,980 KB
testcase_43 AC 40 ms
53,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n=int(input())
ab=[list(map(int,input().split())) for _ in range(n-1)]
ki=[[] for _ in range(n)]
for a,b in ab:
  a,b=a-1,b-1
  ki[a].append(b)
  ki[b].append(a)
# tree_order
todo=[[0,-1]]
order=[]
while todo:
  v,p=todo.pop()
  order.append(v)
  for nv in ki[v]:
    if nv!=p:
      todo.append([nv,v])
order.reverse()
to_ary=[1]*n
seen=[0]*n
ans=[0]*n
for v in order:
  seen[v]=1
  for nv in ki[v]:
    if seen[nv]==0:
      ans[v]+=(n-to_ary[v])*(to_ary[v]) # 根の個数 x サイズ
      to_ary[nv]+=to_ary[v]
    else:
      ans[v]+=(n-to_ary[nv])*(to_ary[nv])
  ans[v]+=n
print(sum(ans))

0