結果

問題 No.1098 LCAs
ユーザー H3PO4H3PO4
提出日時 2020-06-26 22:43:50
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 606 bytes
コンパイル時間 261 ms
コンパイル使用メモリ 87,096 KB
実行使用メモリ 121,476 KB
最終ジャッジ日時 2023-09-18 06:24:40
合計ジャッジ時間 9,747 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
78,136 KB
testcase_01 AC 74 ms
71,272 KB
testcase_02 AC 73 ms
71,108 KB
testcase_03 AC 72 ms
71,088 KB
testcase_04 AC 75 ms
70,944 KB
testcase_05 AC 74 ms
71,140 KB
testcase_06 AC 72 ms
71,124 KB
testcase_07 AC 75 ms
70,948 KB
testcase_08 AC 73 ms
70,952 KB
testcase_09 AC 73 ms
71,088 KB
testcase_10 AC 73 ms
71,048 KB
testcase_11 AC 74 ms
71,124 KB
testcase_12 AC 72 ms
71,164 KB
testcase_13 AC 111 ms
76,276 KB
testcase_14 AC 104 ms
76,780 KB
testcase_15 AC 101 ms
76,456 KB
testcase_16 AC 102 ms
76,316 KB
testcase_17 AC 110 ms
77,700 KB
testcase_18 AC 621 ms
111,040 KB
testcase_19 AC 634 ms
111,420 KB
testcase_20 AC 646 ms
111,880 KB
testcase_21 AC 629 ms
111,040 KB
testcase_22 AC 650 ms
111,204 KB
testcase_23 TLE -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import itertools
import sys

sys.setrecursionlimit(10 ** 9)
int1 = lambda x: int(x) - 1

N = int(input())
T = [[] for _ in range(N)]
for _ in range(N - 1):
    v, w = map(int1, input().split())
    T[v].append(w)
    T[w].append(v)

ans = [1] * N


def rec(child, parent):
    if len(T[child]) <= 1 and parent >= 0:
        return 1

    children = []
    for c in T[child]:
        if c != parent:
            children.append(rec(c, child))

    S = sum(children)
    ans[child] += 2 * S + sum(p * q for p, q in itertools.combinations(children, 2)) * 2
    return S + 1


rec(0, -1)
print(*ans, sep='\n')
0