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?
Input
The first line contains a single integer n (1≤n≤105) — the number of junctions in the tree.
The second line contains n−1 integers p2, p3, …, pn (1≤pi<i), where pi means there is a branch between junctions i and pi. It is guaranteed that this set of branches forms a tree.
Output
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.
Examples
Input
3
1 1
Output
1 1 2
Input
5
1 1 3 3
Output
1 1 1 2 3
Note
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.
題意:
1爲根節點,葉子節點需要染色。一個點稱爲好點當且僅當其所有子樹葉子顏色不同。問滿足個點爲(1≤k≤n)好點時需要的顏色數。
思路:
有一種顏色的時候,只對應一個葉子的節點成爲好點。有兩種顏色的時候,對應兩個葉子的節點可以是好點,以此類推。所以可以維護每個點對應的葉子個數。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <string>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 7;
int f[maxn];
vector<int>G[maxn];
int num[maxn];
void dfs(int u,int fa) {
if(u != 1 && G[u].size() == 1) {
f[u] = 1;
}
for(int i = 0;i < G[u].size();i++) {
int v = G[u][i];
if(v == fa) continue;
dfs(v,u);
f[u] += f[v];
}
}
int main() {
int n;scanf("%d",&n);
for(int i = 2;i <= n;i++) {
int x;scanf("%d",&x);
G[i].push_back(x);
G[x].push_back(i);
}
dfs(1,-1);
for(int i = 1;i <= n;i++) {
num[f[i]]++;
}
for(int i = 1;i <= n;i++) {
num[i] += num[i - 1];
}
int cnt = 1;
for(int i = 1;i <= n;i++) {
if(num[cnt] >= i) {
printf("%d ",cnt);
} else {
while(num[cnt] < i) {
cnt++;
}
printf("%d ",cnt);
}
}
return 0;
}