Olympiad in Informatics

P3830: [SHOI2012] Random Tree — Solution

Approach

Question 1

The first question is fairly easy. Let the state \(f[i]\) be the expected depth when there are \(i\) leaf nodes. Considering that a split from the previous state contributes an extra \(f[i-1]+2\) to the total depth, we know:

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

Question 2

The second question is a lot harder. The key is to set the state \(f[i][j]\): the expected tree height with \(i\) leaf nodes and depth greater than or equal to \(j\). Consider enumerating a \(k\) as the number of leaf nodes in the left subtree, and compute:

\[ 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] \]

The last part removes what was counted twice. The answer is \(\sum_{i = 1}^{n - 1} f[n][i] \).