結果

問題 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  
実行時間 642 ms / 2,000 ms
コード長 710 bytes
コンパイル時間 164 ms
コンパイル使用メモリ 10,832 KB
実行使用メモリ 57,984 KB
最終ジャッジ日時 2023-08-13 11:51:13
合計ジャッジ時間 12,628 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,768 KB
testcase_01 AC 16 ms
7,832 KB
testcase_02 AC 17 ms
7,804 KB
testcase_03 AC 561 ms
25,704 KB
testcase_04 AC 553 ms
25,764 KB
testcase_05 AC 558 ms
25,788 KB
testcase_06 AC 559 ms
25,808 KB
testcase_07 AC 574 ms
25,792 KB
testcase_08 AC 357 ms
20,024 KB
testcase_09 AC 159 ms
13,900 KB
testcase_10 AC 184 ms
14,284 KB
testcase_11 AC 102 ms
11,616 KB
testcase_12 AC 299 ms
17,716 KB
testcase_13 AC 342 ms
19,244 KB
testcase_14 AC 366 ms
19,960 KB
testcase_15 AC 265 ms
16,868 KB
testcase_16 AC 41 ms
9,232 KB
testcase_17 AC 43 ms
9,276 KB
testcase_18 AC 519 ms
24,180 KB
testcase_19 AC 91 ms
11,276 KB
testcase_20 AC 27 ms
8,680 KB
testcase_21 AC 255 ms
16,784 KB
testcase_22 AC 222 ms
15,528 KB
testcase_23 AC 30 ms
8,800 KB
testcase_24 AC 31 ms
8,716 KB
testcase_25 AC 26 ms
8,768 KB
testcase_26 AC 30 ms
8,816 KB
testcase_27 AC 18 ms
8,228 KB
testcase_28 AC 20 ms
8,256 KB
testcase_29 AC 35 ms
9,040 KB
testcase_30 AC 33 ms
8,952 KB
testcase_31 AC 24 ms
8,512 KB
testcase_32 AC 28 ms
8,580 KB
testcase_33 AC 127 ms
19,104 KB
testcase_34 AC 642 ms
57,984 KB
testcase_35 AC 235 ms
28,516 KB
testcase_36 AC 65 ms
9,952 KB
testcase_37 AC 435 ms
22,308 KB
testcase_38 AC 386 ms
20,992 KB
testcase_39 AC 15 ms
7,772 KB
testcase_40 AC 15 ms
7,824 KB
testcase_41 AC 16 ms
7,904 KB
testcase_42 AC 16 ms
7,800 KB
testcase_43 AC 16 ms
7,820 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