結果

問題 No.1098 LCAs
ユーザー tktk_snsntktk_snsn
提出日時 2020-12-15 23:50:38
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,737 ms / 2,000 ms
コード長 832 bytes
コンパイル時間 173 ms
コンパイル使用メモリ 11,860 KB
実行使用メモリ 86,792 KB
最終ジャッジ日時 2023-10-20 07:00:19
合計ジャッジ時間 24,225 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,128 KB
testcase_01 AC 32 ms
10,128 KB
testcase_02 AC 35 ms
10,128 KB
testcase_03 AC 29 ms
10,128 KB
testcase_04 AC 29 ms
10,128 KB
testcase_05 AC 29 ms
10,128 KB
testcase_06 AC 30 ms
10,128 KB
testcase_07 AC 29 ms
10,128 KB
testcase_08 AC 29 ms
10,128 KB
testcase_09 AC 28 ms
10,128 KB
testcase_10 AC 29 ms
10,128 KB
testcase_11 AC 29 ms
10,128 KB
testcase_12 AC 29 ms
10,128 KB
testcase_13 AC 34 ms
10,480 KB
testcase_14 AC 33 ms
10,480 KB
testcase_15 AC 35 ms
10,480 KB
testcase_16 AC 34 ms
10,476 KB
testcase_17 AC 33 ms
10,480 KB
testcase_18 AC 1,607 ms
72,308 KB
testcase_19 AC 1,586 ms
72,288 KB
testcase_20 AC 1,614 ms
72,292 KB
testcase_21 AC 1,612 ms
72,288 KB
testcase_22 AC 1,578 ms
72,308 KB
testcase_23 AC 1,317 ms
66,008 KB
testcase_24 AC 1,306 ms
66,004 KB
testcase_25 AC 1,282 ms
65,124 KB
testcase_26 AC 1,343 ms
71,396 KB
testcase_27 AC 1,335 ms
71,396 KB
testcase_28 AC 1,737 ms
86,792 KB
testcase_29 AC 1,729 ms
86,792 KB
testcase_30 AC 1,720 ms
86,792 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