結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー raven7959raven7959
提出日時 2021-09-25 07:21:19
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 769 ms / 2,000 ms
コード長 769 bytes
コンパイル時間 282 ms
コンパイル使用メモリ 86,692 KB
実行使用メモリ 274,772 KB
最終ジャッジ日時 2023-09-18 22:49:19
合計ジャッジ時間 14,145 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
70,988 KB
testcase_01 AC 72 ms
71,436 KB
testcase_02 AC 73 ms
71,204 KB
testcase_03 AC 398 ms
98,976 KB
testcase_04 AC 395 ms
99,648 KB
testcase_05 AC 399 ms
100,252 KB
testcase_06 AC 435 ms
99,920 KB
testcase_07 AC 386 ms
100,664 KB
testcase_08 AC 295 ms
92,760 KB
testcase_09 AC 210 ms
85,876 KB
testcase_10 AC 216 ms
87,008 KB
testcase_11 AC 184 ms
83,584 KB
testcase_12 AC 283 ms
89,676 KB
testcase_13 AC 305 ms
91,268 KB
testcase_14 AC 320 ms
91,820 KB
testcase_15 AC 274 ms
90,572 KB
testcase_16 AC 165 ms
80,368 KB
testcase_17 AC 152 ms
80,412 KB
testcase_18 AC 381 ms
97,748 KB
testcase_19 AC 173 ms
81,976 KB
testcase_20 AC 143 ms
80,508 KB
testcase_21 AC 274 ms
90,904 KB
testcase_22 AC 229 ms
87,444 KB
testcase_23 AC 141 ms
78,948 KB
testcase_24 AC 150 ms
80,368 KB
testcase_25 AC 135 ms
78,328 KB
testcase_26 AC 141 ms
78,820 KB
testcase_27 AC 86 ms
75,696 KB
testcase_28 AC 100 ms
76,648 KB
testcase_29 AC 153 ms
80,136 KB
testcase_30 AC 147 ms
80,112 KB
testcase_31 AC 134 ms
78,364 KB
testcase_32 AC 140 ms
78,608 KB
testcase_33 AC 243 ms
119,748 KB
testcase_34 AC 769 ms
274,772 KB
testcase_35 AC 346 ms
143,928 KB
testcase_36 AC 125 ms
79,592 KB
testcase_37 AC 241 ms
99,552 KB
testcase_38 AC 233 ms
97,908 KB
testcase_39 AC 72 ms
71,244 KB
testcase_40 AC 74 ms
71,348 KB
testcase_41 AC 73 ms
71,436 KB
testcase_42 AC 72 ms
71,236 KB
testcase_43 AC 71 ms
71,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**9)
N=int(input())
G=[[] for _ in range(N)]
for _ in range(N-1):
  a,b=map(lambda x:int(x)-1,input().split())
  G[a].append(b)
  G[b].append(a)
subtree_size=[1]*N
subtree_size_of_childlen=[[] for _ in range(N)]
def dfs(G,v,p=-1):
  for nextv in G[v]:
    if nextv!=p:
      dfs(G,nextv,v)
  for c in G[v]:
    if c!=p:
      subtree_size[v]+=subtree_size[c]
def dfs2(G,v,p=-1):
  for nextv in G[v]:
    if nextv!=p:
      dfs2(G,nextv,v)
  for c in G[v]:
    if c!=p:
      subtree_size_of_childlen[v].append(subtree_size[c])
dfs(G,0)
dfs2(G,0)
ans=0
for i in range(N):
  # print(i)
  ans+=(N-subtree_size[i])*subtree_size[i]
  # print(ans)
  for num in subtree_size_of_childlen[i]:
    ans+=(N-num)*num
  # print(ans)
print(ans+N*N)
0