結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー titiatitia
提出日時 2021-03-05 21:54:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 226 ms / 2,000 ms
コード長 750 bytes
コンパイル時間 175 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 102,424 KB
最終ジャッジ日時 2024-04-16 09:20:24
合計ジャッジ時間 6,327 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
52,224 KB
testcase_01 AC 42 ms
51,712 KB
testcase_02 AC 41 ms
52,224 KB
testcase_03 AC 223 ms
94,464 KB
testcase_04 AC 220 ms
95,732 KB
testcase_05 AC 218 ms
96,128 KB
testcase_06 AC 214 ms
95,828 KB
testcase_07 AC 226 ms
96,080 KB
testcase_08 AC 163 ms
88,960 KB
testcase_09 AC 111 ms
81,536 KB
testcase_10 AC 114 ms
81,408 KB
testcase_11 AC 100 ms
79,104 KB
testcase_12 AC 136 ms
84,224 KB
testcase_13 AC 157 ms
88,192 KB
testcase_14 AC 167 ms
89,344 KB
testcase_15 AC 132 ms
83,968 KB
testcase_16 AC 80 ms
71,808 KB
testcase_17 AC 80 ms
71,936 KB
testcase_18 AC 198 ms
92,288 KB
testcase_19 AC 94 ms
78,080 KB
testcase_20 AC 72 ms
68,608 KB
testcase_21 AC 131 ms
83,456 KB
testcase_22 AC 124 ms
82,432 KB
testcase_23 AC 74 ms
69,888 KB
testcase_24 AC 74 ms
68,480 KB
testcase_25 AC 73 ms
68,992 KB
testcase_26 AC 72 ms
68,352 KB
testcase_27 AC 49 ms
59,008 KB
testcase_28 AC 64 ms
65,024 KB
testcase_29 AC 76 ms
69,888 KB
testcase_30 AC 77 ms
70,400 KB
testcase_31 AC 69 ms
66,944 KB
testcase_32 AC 72 ms
68,480 KB
testcase_33 AC 96 ms
79,744 KB
testcase_34 AC 192 ms
93,824 KB
testcase_35 AC 111 ms
82,816 KB
testcase_36 AC 73 ms
72,832 KB
testcase_37 AC 157 ms
102,424 KB
testcase_38 AC 155 ms
99,544 KB
testcase_39 AC 40 ms
51,968 KB
testcase_40 AC 40 ms
52,352 KB
testcase_41 AC 40 ms
51,968 KB
testcase_42 AC 40 ms
52,480 KB
testcase_43 AC 41 ms
51,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N=int(input())
E=[[] for i in range(N+1)]

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

ROOT=1

QUE=[ROOT] 
Parent=[-1]*(N+1)
Parent[ROOT]=0 # ROOTの親を定めておく.
TOP_SORT=[] # トポロジカルソート

while QUE: # トポロジカルソートと同時に親を見つける
    x=QUE.pop()
    TOP_SORT.append(x)
    for to in E[x]:
        if Parent[to]==-1:
            Parent[to]=x
            QUE.append(to)

Children=[1]*(N+1)

for x in TOP_SORT[::-1]: #(自分を含む)子ノードの数を調べる
    Children[Parent[x]]+=Children[x]

ANS=N*N
for i in range(1,N+1):
    x=Children[i]-1
    y=N-x-1

    ANS+=(x+1)*y+y*(x+1)

print(ANS)
0