結果

問題 No.1098 LCAs
コンテスト
ユーザー Kohei
提出日時 2026-07-30 21:14:46
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 764 ms / 2,000 ms
+ 661µs
コード長 850 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 814 ms
コンパイル使用メモリ 95,976 KB
実行使用メモリ 347,776 KB
最終ジャッジ日時 2026-07-30 21:15:03
合計ジャッジ時間 11,718 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys
sys.setrecursionlimit(10**8)
import pypyjit
pypyjit.set_param('max_unroll_recursion=-1')
N = int(input())
G = [list() for _ in range(N)]
for i in range(N - 1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    G[u].append(v)
    G[v].append(u)

cnt = [1]*N
ans = [0]*N
def dfs(pos, pre):
    for nex in G[pos]:
        if nex == pre:
            continue
        dfs(nex, pos)
        # 親に計上
        cnt[pos] += cnt[nex]

    # pos の子どもの計算はすべて終わっている
    res = 0
    childrentotal = cnt[pos] - 1
    # 子ども同士
    for nex in G[pos]:
        if nex == pre:
            continue
        res += cnt[nex] * (childrentotal - cnt[nex])
    # 親と子ども(双方向)
    res += 1 * childrentotal * 2
    # 親と親
    res += 1
    ans[pos] = res

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