結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー ygd.ygd.
提出日時 2021-03-05 22:31:21
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 730 ms / 2,000 ms
コード長 710 bytes
コンパイル時間 147 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 60,664 KB
最終ジャッジ日時 2024-05-01 05:09:09
合計ジャッジ時間 12,697 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
10,752 KB
testcase_01 AC 29 ms
10,880 KB
testcase_02 AC 30 ms
10,752 KB
testcase_03 AC 679 ms
28,288 KB
testcase_04 AC 673 ms
28,288 KB
testcase_05 AC 669 ms
28,416 KB
testcase_06 AC 670 ms
28,288 KB
testcase_07 AC 673 ms
28,416 KB
testcase_08 AC 442 ms
22,656 KB
testcase_09 AC 203 ms
16,384 KB
testcase_10 AC 229 ms
16,896 KB
testcase_11 AC 131 ms
14,208 KB
testcase_12 AC 337 ms
20,224 KB
testcase_13 AC 398 ms
21,888 KB
testcase_14 AC 424 ms
22,400 KB
testcase_15 AC 311 ms
19,456 KB
testcase_16 AC 59 ms
11,776 KB
testcase_17 AC 60 ms
11,904 KB
testcase_18 AC 588 ms
26,752 KB
testcase_19 AC 118 ms
13,952 KB
testcase_20 AC 43 ms
11,264 KB
testcase_21 AC 308 ms
19,200 KB
testcase_22 AC 271 ms
18,176 KB
testcase_23 AC 47 ms
11,520 KB
testcase_24 AC 49 ms
11,520 KB
testcase_25 AC 43 ms
11,136 KB
testcase_26 AC 48 ms
11,392 KB
testcase_27 AC 33 ms
10,880 KB
testcase_28 AC 35 ms
10,880 KB
testcase_29 AC 53 ms
11,648 KB
testcase_30 AC 52 ms
11,520 KB
testcase_31 AC 41 ms
11,136 KB
testcase_32 AC 46 ms
11,392 KB
testcase_33 AC 162 ms
21,756 KB
testcase_34 AC 730 ms
60,664 KB
testcase_35 AC 292 ms
31,096 KB
testcase_36 AC 86 ms
12,672 KB
testcase_37 AC 502 ms
24,832 KB
testcase_38 AC 453 ms
23,808 KB
testcase_39 AC 29 ms
10,880 KB
testcase_40 AC 29 ms
10,880 KB
testcase_41 AC 29 ms
10,880 KB
testcase_42 AC 29 ms
10,752 KB
testcase_43 AC 29 ms
10,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(500000)

N = int(input())
G = [[] for _ in range(N)]
for _ in range(N-1):
    A,B = map(int,input().split())
    A-=1;B-=1 #0-index
    G[A].append(B)
    G[B].append(A)

P = [-1]*N
def dfs(v,par=-1):
    P[v] = 1
    for u in G[v]:
        if u == par: continue
        P[v] += dfs(u,v)
    return P[v]
dfs(0)
#print(P)
#S = sum(P)
global ans
ans = 0
def dfs2(v,par,S): #Sは今の合計
    global ans
    ans += S
    for u in G[v]:
        if u == par: continue
        pre_v = P[v]
        pre_u = P[u]
        P[v] = N - P[u]
        P[u] = N
        new_S = S - pre_u + P[v]
        dfs2(u,v,new_S)
        P[v] = pre_v
        P[u] = pre_u

dfs2(0,-1,sum(P))
print(ans)
0