#P7728. The Growing Lunar Tree
The Growing Lunar Tree
The Growing Lunar Tree
A tree grows on the Moon in a very strange way under the influence of moonlight and lunar storms. Initially, you are given a rooted tree \(T_0\) with \(n\) nodes (node 1 is the root). The depth of a node is defined as the number of edges on the simple path from the node to the root.
On the \(i\)th day, the tree grows from \(T_{i-1}\) to \(T_i\) as follows. Let \(v\) be any leaf in \(T_{i-1}\) having the minimum depth (if there are several, you may choose arbitrarily one). Replace the node \(v\) with an exact copy of \(T_{i-1}\) (the entire current tree). That is, the new subtree attached where \(v\) was is a copy of \(T_{i-1}\); note that in the copy the depth of a node is increased by the depth of \(v\).
For every integer \(d\) from \(1\) to \(m\), define \(S_d\) to be the minimum number of days such that in \(T_{S_d}\) every leaf has depth strictly greater than \(d\) (i.e. the minimum leaf depth is \(> d\)). Since the answer can be huge, output each \(S_d\) modulo \(998244353\).
Note: The evolution rule means that in one day, you remove exactly one occurrence of the current shallowest leaf and add a copy of the tree (shifted by the depth at which the replacement occurs). Although the tree grows very quickly, you only need to compute the day count at which the minimum leaf depth becomes greater than a given number \(d\). The initial "coins" (or increments) are exactly the leaf depths of \(T_0\). Thus the process can be viewed as starting from a multiset \(M_0\) containing the leaf depths of \(T_0\) and, in each step, removing the smallest number \(x\) and inserting \(x+c\) for every \(c\) in \(M_0\>.
inputFormat
The first line contains two integers \(n\) and \(m\) \((1 \leq n \leq 100,\; 1 \leq m \leq 1000)\) -- the number of nodes in the initial tree and the maximum \(d\) you care about. The next \(n-1\) lines contain one integer each. For \(i=2,3,\ldots,n\), the \(i\)th node's parent is given by an integer \(p_i\) \((1 \leq p_i < i)\). It is guaranteed that the given tree is rooted at node 1.
outputFormat
Output \(m\) integers: \(S_1, S_2, \dots, S_m\) (each modulo \(998244353\)), where \(S_d\) is the minimum number of days such that every leaf in \(T_{S_d}\) has depth greater than \(d\).
sample
3 5
1
1
0 1 1 2 2