結果

問題 No.1098 LCAs
ユーザー Navier_Boltzmann
提出日時 2023-09-21 13:49:54
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,126 bytes
コンパイル時間 185 ms
コンパイル使用メモリ 81,976 KB
実行使用メモリ 132,328 KB
最終ジャッジ日時 2024-07-07 07:36:16
合計ジャッジ時間 10,330 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25 RE * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

import pypyjit
pypyjit.set_param('max_unroll_recursion=-1')
from collections import *
from itertools import *
from functools import *
from heapq import *
import sys,math
input = sys.stdin.readline

N = int(input())
e = [[] for _ in range(N)]
for _ in range(N-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    e[a].append(b)
    e[b].append(a)
    
X = [[] for _ in range(N)]
vis = [False]*N
vis[0]=True
v = deque()
v.append(0)
parents = [-1]*N
sub = [-1]*N
def dfs(x):
    tmp = 1
    if len(e[x]) == 1 and x != 0:
        sub[x] = 1
        return 1
    for ix in e[x]:
        if ix == parents[x]:
            continue
        parents[ix] = x
        if sub[ix] != -1:
            tmp += sub[ix]
        else:
            tmp += dfs(ix)
    sub[x] = tmp
    return tmp
dfs(0)
while v:
    
    x = v.popleft()
    
    for ix in e[x]:
        
        if vis[ix]:
            continue
        
        X[x].append(sub[ix])
        vis[ix]=True
        v.append(ix)

for i in range(N):
    
    s2 = sum(j**2 for j in X[i])
    s = sum(j for j in X[i])
    ans = (s**2 - s2) + 2*s + 1
    print(ans)
# print(X)
0