結果

問題 No.1098 LCAs
ユーザー brthyyjpbrthyyjp
提出日時 2020-12-26 15:33:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 455 ms / 2,000 ms
コード長 734 bytes
コンパイル時間 164 ms
コンパイル使用メモリ 81,856 KB
実行使用メモリ 141,872 KB
最終ジャッジ日時 2023-10-24 23:32:58
合計ジャッジ時間 9,528 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,652 KB
testcase_01 AC 35 ms
53,652 KB
testcase_02 AC 35 ms
53,652 KB
testcase_03 AC 35 ms
53,652 KB
testcase_04 AC 36 ms
53,652 KB
testcase_05 AC 34 ms
53,652 KB
testcase_06 AC 36 ms
53,652 KB
testcase_07 AC 35 ms
53,652 KB
testcase_08 AC 35 ms
53,652 KB
testcase_09 AC 36 ms
53,652 KB
testcase_10 AC 37 ms
53,652 KB
testcase_11 AC 36 ms
53,652 KB
testcase_12 AC 36 ms
53,652 KB
testcase_13 AC 55 ms
66,336 KB
testcase_14 AC 55 ms
66,308 KB
testcase_15 AC 54 ms
66,308 KB
testcase_16 AC 54 ms
66,308 KB
testcase_17 AC 54 ms
66,308 KB
testcase_18 AC 414 ms
122,952 KB
testcase_19 AC 432 ms
126,860 KB
testcase_20 AC 429 ms
126,876 KB
testcase_21 AC 409 ms
126,592 KB
testcase_22 AC 430 ms
126,592 KB
testcase_23 AC 357 ms
139,184 KB
testcase_24 AC 348 ms
139,924 KB
testcase_25 AC 325 ms
140,756 KB
testcase_26 AC 335 ms
141,872 KB
testcase_27 AC 324 ms
141,316 KB
testcase_28 AC 451 ms
127,008 KB
testcase_29 AC 455 ms
127,016 KB
testcase_30 AC 450 ms
127,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

n = int(input())
g = [[] for _ in range(n)]
for i in range(n-1):
    v, w = map(int, input().split())
    v, w = v-1, w-1
    g[v].append(w)
    g[w].append(v)

s = []
order = []
parent = [-1]*n
s.append(0)
while s:
    v = s.pop()
    order.append(v)
    for u in g[v]:
        if parent[v] != u:
            s.append(u)
            parent[u] = v
order.reverse()
C = [1]*n
for v in order:
    if parent[v] != -1:
        C[parent[v]] += C[v]
#print(C)
ans = [0]*n
for v in order:
    for u in g[v]:
        if u == parent[v]:
            continue
        ans[v] += 2*C[u]
        ans[v] += (C[v]-1-C[u])*C[u]
    ans[v] += 1
print(*ans, sep='\n')
0