結果

問題 No.1098 LCAs
コンテスト
ユーザー H3PO4
提出日時 2020-06-26 22:43:14
言語 Python3
(3.14.3 + numpy 2.4.4 + scipy 1.17.1)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
TLE  
実行時間 -
コード長 606 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 690 ms
コンパイル使用メモリ 20,824 KB
実行使用メモリ 156,468 KB
最終ジャッジ日時 2026-03-26 05:07:31
合計ジャッジ時間 27,729 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23 TLE * 5
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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