結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー roarisroaris
提出日時 2021-03-05 21:52:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 516 ms / 2,000 ms
コード長 614 bytes
コンパイル時間 227 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 182,144 KB
最終ジャッジ日時 2024-04-16 09:19:04
合計ジャッジ時間 8,851 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
53,632 KB
testcase_01 AC 46 ms
53,760 KB
testcase_02 AC 47 ms
53,760 KB
testcase_03 AC 327 ms
90,368 KB
testcase_04 AC 346 ms
91,084 KB
testcase_05 AC 338 ms
90,368 KB
testcase_06 AC 324 ms
89,856 KB
testcase_07 AC 328 ms
90,496 KB
testcase_08 AC 252 ms
86,144 KB
testcase_09 AC 162 ms
80,896 KB
testcase_10 AC 177 ms
82,048 KB
testcase_11 AC 136 ms
79,360 KB
testcase_12 AC 219 ms
84,352 KB
testcase_13 AC 241 ms
85,888 KB
testcase_14 AC 256 ms
86,528 KB
testcase_15 AC 212 ms
84,096 KB
testcase_16 AC 109 ms
77,440 KB
testcase_17 AC 109 ms
77,184 KB
testcase_18 AC 326 ms
89,216 KB
testcase_19 AC 134 ms
79,232 KB
testcase_20 AC 104 ms
77,824 KB
testcase_21 AC 205 ms
83,200 KB
testcase_22 AC 205 ms
82,304 KB
testcase_23 AC 102 ms
76,928 KB
testcase_24 AC 107 ms
77,440 KB
testcase_25 AC 89 ms
74,624 KB
testcase_26 AC 101 ms
77,056 KB
testcase_27 AC 58 ms
62,592 KB
testcase_28 AC 67 ms
66,816 KB
testcase_29 AC 102 ms
77,312 KB
testcase_30 AC 100 ms
77,056 KB
testcase_31 AC 86 ms
73,600 KB
testcase_32 AC 95 ms
76,928 KB
testcase_33 AC 152 ms
94,720 KB
testcase_34 AC 516 ms
182,144 KB
testcase_35 AC 231 ms
115,968 KB
testcase_36 AC 90 ms
77,980 KB
testcase_37 AC 206 ms
91,008 KB
testcase_38 AC 196 ms
89,856 KB
testcase_39 AC 47 ms
53,888 KB
testcase_40 AC 46 ms
53,504 KB
testcase_41 AC 46 ms
53,504 KB
testcase_42 AC 46 ms
53,632 KB
testcase_43 AC 47 ms
53,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**5+10)
from collections import *

def dfs1(v, pv):
    for nv in G[v]:
        if nv==pv:
            continue
        
        dfs1(nv, v)
        num[v] += num[nv]

def dfs2(v, pv):
    for nv in G[v]:
        if nv==pv:
            continue
        
        ans[nv] = ans[v]+N-2*num[nv]
        dfs2(nv, v)
    
N = int(input())
G = [[] for _ in range(N)]

for _ in range(N-1):
    A, B = map(int, input().split())
    G[A-1].append(B-1)
    G[B-1].append(A-1)
    
num = [1]*N
dfs1(0, -1)
ans = [-1]*N
ans[0] = sum(num)
dfs2(0, -1)
print(sum(ans))
0