結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー 👑 potato167potato167
提出日時 2022-02-17 01:23:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 361 ms / 2,000 ms
コード長 520 bytes
コンパイル時間 396 ms
コンパイル使用メモリ 86,772 KB
実行使用メモリ 98,904 KB
最終ジャッジ日時 2023-09-11 17:21:14
合計ジャッジ時間 11,913 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,160 KB
testcase_01 AC 72 ms
71,424 KB
testcase_02 AC 72 ms
71,424 KB
testcase_03 AC 359 ms
97,320 KB
testcase_04 AC 344 ms
97,852 KB
testcase_05 AC 348 ms
97,232 KB
testcase_06 AC 354 ms
97,192 KB
testcase_07 AC 361 ms
97,264 KB
testcase_08 AC 268 ms
90,660 KB
testcase_09 AC 183 ms
83,212 KB
testcase_10 AC 183 ms
84,228 KB
testcase_11 AC 156 ms
80,572 KB
testcase_12 AC 231 ms
87,764 KB
testcase_13 AC 255 ms
90,108 KB
testcase_14 AC 258 ms
90,588 KB
testcase_15 AC 213 ms
86,712 KB
testcase_16 AC 134 ms
78,924 KB
testcase_17 AC 129 ms
78,412 KB
testcase_18 AC 320 ms
95,788 KB
testcase_19 AC 145 ms
79,316 KB
testcase_20 AC 124 ms
78,256 KB
testcase_21 AC 204 ms
85,900 KB
testcase_22 AC 194 ms
84,852 KB
testcase_23 AC 123 ms
78,280 KB
testcase_24 AC 124 ms
78,532 KB
testcase_25 AC 124 ms
77,980 KB
testcase_26 AC 125 ms
78,072 KB
testcase_27 AC 88 ms
75,644 KB
testcase_28 AC 103 ms
76,676 KB
testcase_29 AC 131 ms
78,628 KB
testcase_30 AC 128 ms
78,188 KB
testcase_31 AC 123 ms
77,968 KB
testcase_32 AC 126 ms
78,248 KB
testcase_33 AC 149 ms
81,068 KB
testcase_34 AC 282 ms
97,136 KB
testcase_35 AC 183 ms
85,320 KB
testcase_36 AC 125 ms
78,608 KB
testcase_37 AC 234 ms
98,904 KB
testcase_38 AC 220 ms
95,580 KB
testcase_39 AC 72 ms
71,324 KB
testcase_40 AC 72 ms
71,500 KB
testcase_41 AC 70 ms
70,952 KB
testcase_42 AC 73 ms
71,316 KB
testcase_43 AC 73 ms
71,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N=int(input())
G=[[]for i in range(N)]
for i in range(N-1):
	a,b=map(int,input().split())
	a-=1
	b-=1
	G[a].append(b)
	G[b].append(a)
order=[0]
pare=[-1]*N
pare[0]=-2
size=[0]*N
dp=[0]*N
for i in range(N):
	a=order[i]
	for x in G[a]:
		if pare[x]==-1:
			pare[x]=a
			order.append(x)
for i in range(N):
	a=order[N-1-i]
	for x in G[a]:
		if x!=pare[a]:
			size[a]+=size[x]
	size[a]+=1
	dp[0]+=size[a]
ans=0
for i in range(N):
	a=order[i]
	for x in G[a]:
		if pare[x]==a:
			dp[x]=dp[a]+N-2*size[x]
	ans+=dp[a]
print(ans)
0