結果

問題 No.1098 LCAs
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2023-09-21 13:52:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,696 ms / 2,000 ms
コード長 1,155 bytes
コンパイル時間 433 ms
コンパイル使用メモリ 87,308 KB
実行使用メモリ 291,840 KB
最終ジャッジ日時 2023-09-21 13:52:45
合計ジャッジ時間 20,465 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 111 ms
72,880 KB
testcase_01 AC 120 ms
72,900 KB
testcase_02 AC 111 ms
72,876 KB
testcase_03 AC 113 ms
72,812 KB
testcase_04 AC 111 ms
72,904 KB
testcase_05 AC 112 ms
72,948 KB
testcase_06 AC 111 ms
72,904 KB
testcase_07 AC 112 ms
72,936 KB
testcase_08 AC 111 ms
72,848 KB
testcase_09 AC 114 ms
72,872 KB
testcase_10 AC 121 ms
73,016 KB
testcase_11 AC 110 ms
72,940 KB
testcase_12 AC 117 ms
72,852 KB
testcase_13 AC 145 ms
78,016 KB
testcase_14 AC 139 ms
78,292 KB
testcase_15 AC 143 ms
78,068 KB
testcase_16 AC 142 ms
78,288 KB
testcase_17 AC 139 ms
78,200 KB
testcase_18 AC 1,065 ms
135,212 KB
testcase_19 AC 1,036 ms
135,172 KB
testcase_20 AC 1,051 ms
135,252 KB
testcase_21 AC 1,108 ms
134,852 KB
testcase_22 AC 1,115 ms
135,268 KB
testcase_23 AC 858 ms
135,036 KB
testcase_24 AC 833 ms
134,852 KB
testcase_25 AC 788 ms
132,784 KB
testcase_26 AC 818 ms
132,836 KB
testcase_27 AC 785 ms
133,024 KB
testcase_28 AC 1,648 ms
288,712 KB
testcase_29 AC 1,696 ms
291,840 KB
testcase_30 AC 1,371 ms
270,636 KB
権限があれば一括ダウンロードができます

ソースコード

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
sys.setrecursionlimit(10**6)
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