A12112 | Decorate Apple Tree
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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$ .
A leaf is such a junction that its subtree contains exactly one junction.
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.
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$ ?
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$ .
A leaf is such a junction that its subtree contains exactly one junction.
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.
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$ ?
输入格式
The first line contains a single integer $n$ ( $1 \le n \le 10^5$ ) — the number of junctions in the tree.
The second line contains $n - 1$ integers $p_2$ , $p_3$ , ..., $p_n$ ( $1 \le p_i < 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.
The second line contains $n - 1$ integers $p_2$ , $p_3$ , ..., $p_n$ ( $1 \le p_i < 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.
输出格式
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$ .
输入输出样例
输入 #1
3 1 1
输出 #1
1 1 2
输入 #2
5 1 1 3 3
输出 #2
1 1 1 2 3
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.
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.
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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted