---
id: 1223
title: "P3830:「SHOI2012」随机树题解"
slug: p3830-shoi2012-random-tree
lang: zh
date: 2019-04-24T12:05:55
date_gmt: 2019-04-24T04:05:55
modified: 2023-11-11T14:50:30
status: publish
password_protected: false
category: { name: Olympiad in Informatics, slug: oi }
tags: [OI, DP, 概率期望]
original_url: https://kalorona.com/oi/p3830shoi2012随机树题解/
archived: 2026-11-27
editor: classic
media: []
archive_notes:
  - "Body headings were <h1>/<h2> in the original; shifted down one level (h1→h2, h2→h3) so the page title is the only h1."
  - "The live page showed en dashes for the minus signs in the second-question formulas because of WordPress texturize; the ASCII - from the database source was kept."
comments: []
---

## 解法

### 第一问

第一问比较好求，设状态$f[i]$为有$i$个叶子节点时的期望深度。考虑从上一个状态分裂会多对总深度做$f[i-1]+2$的贡献，可以知道：

$$
f[i] = \frac{f[i-1] * (i-1) + f[i-1] + 2}{i} = f[i-1] + \frac{2}{i}
$$

<!--more-->

### 第二问

第二问难了很多，重点设状态$f[i][j]$：有$i$个叶子节点深度大于等于$j$的期望树高。考虑枚举一个$k$为左子树叶子结点的个数，计算：

$$
f[i][j] = \sum_{k = 1}^{i - 1} f[k][j - 1] + f[i - k][j - 1] - f[k][j - 1] * f[i - k][j - 1]
$$

后面删掉重复计算的部分。答案是$\sum_{i = 1}^{n - 1} f[n][i]$。
