結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
75,668 KB
testcase_01 AC 73 ms
71,460 KB
testcase_02 AC 74 ms
71,428 KB
testcase_03 AC 74 ms
71,588 KB
testcase_04 AC 74 ms
71,292 KB
testcase_05 AC 73 ms
71,472 KB
testcase_06 AC 76 ms
71,096 KB
testcase_07 AC 74 ms
71,200 KB
testcase_08 AC 74 ms
71,244 KB
testcase_09 AC 74 ms
71,500 KB
testcase_10 AC 73 ms
71,412 KB
testcase_11 AC 75 ms
71,472 KB
testcase_12 AC 74 ms
71,332 KB
testcase_13 AC 102 ms
76,720 KB
testcase_14 AC 107 ms
76,648 KB
testcase_15 AC 102 ms
76,852 KB
testcase_16 AC 103 ms
76,652 KB
testcase_17 AC 112 ms
77,848 KB
testcase_18 AC 614 ms
111,424 KB
testcase_19 AC 637 ms
111,600 KB
testcase_20 AC 626 ms
111,700 KB
testcase_21 AC 623 ms
111,184 KB
testcase_22 AC 630 ms
111,600 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 ** 6)
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