結果
| 問題 | No.1098 LCAs |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-07-30 21:14:46 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
AC
|
| 実行時間 | 764 ms / 2,000 ms |
| + 661µs | |
| コード長 | 850 bytes |
| 記録 | |
| コンパイル時間 | 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 |
ソースコード
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')