結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー AEn
提出日時 2022-12-07 22:47:27
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
RE  
実行時間 -
コード長 871 bytes
コンパイル時間 293 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2024-10-14 02:17:38
合計ジャッジ時間 3,525 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample RE * 3
other RE * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

from sklearn.utils import resample
sys.setrecursionlimit(10**8)

N = int(input())
G = [list() for _ 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)

sub_tree = [0]*N
def dfs(v,p):
    for next_v in G[v]:
        if next_v == p:
            continue
        dfs(next_v, v)
    sub_tree[v] = 1
    for c in G[v]:
        if c== p:
            continue
        sub_tree[v] += sub_tree[c]

def count(v,p,sm):
    for nex in G[v]:
        if nex==p:continue
        v1, v2 = sub_tree[v], sub_tree[nex]
        sm += v1-2*v2
        sub_tree[v] = v1-v2
        sub_tree[nex] = v1
        global res
        res += sm
        count(nex,v,sm)
        sub_tree[v] = v1
        sub_tree[nex] = v2
        sm -= v1-2*v2
        
dfs(0,-1)
res = sum(sub_tree)
count(0,-1,sum(sub_tree))
print(res)
0