結果

問題 No.1098 LCAs
ユーザー tktk_snsntktk_snsn
提出日時 2020-12-15 23:51:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 608 ms / 2,000 ms
コード長 832 bytes
コンパイル時間 354 ms
コンパイル使用メモリ 81,660 KB
実行使用メモリ 161,540 KB
最終ジャッジ日時 2023-10-20 07:00:31
合計ジャッジ時間 10,039 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,392 KB
testcase_01 AC 38 ms
53,392 KB
testcase_02 AC 38 ms
53,392 KB
testcase_03 AC 38 ms
53,392 KB
testcase_04 AC 39 ms
53,392 KB
testcase_05 AC 39 ms
53,392 KB
testcase_06 AC 38 ms
53,392 KB
testcase_07 AC 39 ms
53,392 KB
testcase_08 AC 40 ms
53,392 KB
testcase_09 AC 38 ms
53,392 KB
testcase_10 AC 39 ms
53,392 KB
testcase_11 AC 39 ms
53,392 KB
testcase_12 AC 39 ms
53,392 KB
testcase_13 AC 60 ms
66,096 KB
testcase_14 AC 56 ms
64,004 KB
testcase_15 AC 56 ms
66,060 KB
testcase_16 AC 58 ms
66,100 KB
testcase_17 AC 56 ms
64,004 KB
testcase_18 AC 576 ms
127,948 KB
testcase_19 AC 565 ms
128,104 KB
testcase_20 AC 578 ms
128,256 KB
testcase_21 AC 571 ms
128,108 KB
testcase_22 AC 567 ms
128,072 KB
testcase_23 AC 469 ms
140,420 KB
testcase_24 AC 479 ms
140,420 KB
testcase_25 AC 420 ms
160,268 KB
testcase_26 AC 445 ms
161,540 KB
testcase_27 AC 435 ms
161,536 KB
testcase_28 AC 604 ms
134,356 KB
testcase_29 AC 598 ms
134,364 KB
testcase_30 AC 608 ms
134,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

N = int(input())
G = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)

topo = []
par = [-1] * N
node = [0]
while node:
    s = node.pop()
    topo.append(s)
    for t in G[s]:
        if t == par[s]:
            continue
        par[t] = s
        node.append(t)

ans = [0] * N
child = [[] for _ in range(N)]
size = [1] * N
for s in topo[::-1]:
    for t in G[s]:
        if t == par[s]:
            size[t] += size[s]
        else:
            child[s].append(t)
    cnt = 0
    for c in child[s]:
        sz = size[c]
        ans[s] += 2 * sz
        ans[s] -= sz * sz
        cnt += sz
    ans[s] += cnt * cnt
    ans[s] += 1  # (s, s)

print(*ans, sep="\n")
0