結果

問題 No.1098 LCAs
ユーザー anagohirameanagohirame
提出日時 2020-06-26 21:46:15
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,748 ms / 2,000 ms
コード長 551 bytes
コンパイル時間 248 ms
コンパイル使用メモリ 10,824 KB
実行使用メモリ 94,872 KB
最終ジャッジ日時 2023-09-18 04:00:08
合計ジャッジ時間 20,081 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
7,724 KB
testcase_01 AC 16 ms
7,800 KB
testcase_02 AC 16 ms
7,888 KB
testcase_03 AC 16 ms
7,788 KB
testcase_04 AC 16 ms
7,936 KB
testcase_05 AC 17 ms
7,804 KB
testcase_06 AC 17 ms
7,840 KB
testcase_07 AC 16 ms
7,856 KB
testcase_08 AC 16 ms
7,852 KB
testcase_09 AC 16 ms
7,724 KB
testcase_10 AC 16 ms
7,832 KB
testcase_11 AC 16 ms
7,936 KB
testcase_12 AC 16 ms
7,900 KB
testcase_13 AC 20 ms
8,480 KB
testcase_14 AC 20 ms
8,548 KB
testcase_15 AC 20 ms
8,492 KB
testcase_16 AC 21 ms
8,556 KB
testcase_17 AC 21 ms
8,464 KB
testcase_18 AC 1,363 ms
65,996 KB
testcase_19 AC 1,317 ms
65,572 KB
testcase_20 AC 1,330 ms
65,924 KB
testcase_21 AC 1,339 ms
65,888 KB
testcase_22 AC 1,321 ms
65,840 KB
testcase_23 AC 1,130 ms
59,448 KB
testcase_24 AC 1,135 ms
59,552 KB
testcase_25 AC 1,119 ms
58,480 KB
testcase_26 AC 1,187 ms
64,956 KB
testcase_27 AC 1,186 ms
64,836 KB
testcase_28 AC 1,707 ms
91,720 KB
testcase_29 AC 1,748 ms
94,872 KB
testcase_30 AC 1,714 ms
94,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**6)
n = int(input())
adj = [[] for _ in range(n)]
children = [[] for _ in range(n)]
ans = [0 for _ in range(n)]
for _ in range(n-1):
    u, v = map(int, input().split())
    adj[u-1].append(v-1)
    adj[v-1].append(u-1)

def dfs(x, p):
    for v in adj[x]:
        if v == p:
            continue
        dfs(v, x)
    s = sum(children[x])
    res = 2*s+1
    for i in children[x]:
        res += i * (s-i)
    if x != 0:
        children[p].append(s+1)
    ans[x] = res
    return

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