1056D.md (2233B)
1 Source: https://codeforces.com/problemset/problem/1056/D 2 3 D. Decorate Apple Tree 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 There is one apple tree in Arkady's garden. It can be represented as a set of junctions connected with branches so that there is only one way to reach any junctions from any other one using branches. The junctions are enumerated from $1$ to $n$, the junction $1$ is called the root. 22 23 A subtree of a junction $v$ is a set of junctions $u$ such that the path from $u$ to the root must pass through $v$. Note that $v$ itself is included in a subtree of $v$. 24 25 A leaf is such a junction that its subtree contains exactly one junction. 26 27 The New Year is coming, so Arkady wants to decorate the tree. He will put a light bulb of some color on each leaf junction and then count the number happy junctions. A happy junction is such a junction $t$ that all light bulbs in the subtree of $t$ have different colors. 28 29 Arkady is interested in the following question: for each $k$ from $1$ to $n$, what is the minimum number of different colors needed to make the number of happy junctions be greater than or equal to $k$? 30 31 Input 32 33 The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the number of junctions in the tree. 34 35 The second line contains $n - 1$ integers $p_2$, $p_3$, ..., $p_n$ ($1 \le p_i \lt i$), where $p_i$ means there is a branch between junctions $i$ and $p_i$. It is guaranteed that this set of branches forms a tree. 36 37 Output 38 39 Output $n$ integers. The $i$-th of them should be the minimum number of colors needed to make the number of happy junctions be at least $i$. 40 41 Examples 42 43 Input 44 45 ``` 46 3 47 1 1 48 ``` 49 50 Output 51 52 ``` 53 1 1 2 54 ``` 55 56 Input 57 58 ``` 59 5 60 1 1 3 3 61 ``` 62 63 Output 64 65 ``` 66 1 1 1 2 3 67 ``` 68 69 Note 70 71 In the first example for $k = 1$ and $k = 2$ we can use only one color: the junctions $2$ and $3$ will be happy. For $k = 3$ you have to put the bulbs of different colors to make all the junctions happy. 72 73 In the second example for $k = 4$ you can, for example, put the bulbs of color $1$ in junctions $2$ and $4$, and a bulb of color $2$ into junction $5$. The happy junctions are the ones with indices $2$, $3$, $4$ and $5$ then.