- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- #define endl "\n"
- #define quick ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
- #define ALL(x) x.begin(),x.end()
- #define SORT(x) sort(ALL(x))
- #define FOR(a,b,c) for(ll (a)= (b);(a)<(c);++(a))
- #define REP(a,b) FOR(a,0,b)
- #define mp make_pair
- #define pb push_back
- #define pll pair<ll,ll>
- #define pii pair<int,int>
- #define vl vector<ll>
- #define vi vector<int>
- #define f first
- #define s second
- #define yy cout<<"YES"<<endl;return
- #define nn cout<<"NO"<<endl;return
- #define minus1 cout<<-1<<endl;return
- #define rd(a,x) REP(i,x)cin >> a[i]
- int ttt = 1, case_ = 0;
- ll n, m, k;
- vector<vl> G;
- vl p;
- vl d;
- vl b;
- template <class myType>
- void print_arr(myType &arr, ll L, string sep){
- REP(i,L){ cout << arr[i] << (i < L-1 ? sep : "\n"); }
- return; }
- /*
- */
- void dfs(ll u) {
- for(ll v: G[u]) {
- if (v == p[u]) continue;
- dfs(v);
- if (d[u] < d[v] + 1) {
- b[u] = v;
- d[u] = d[v] + 1;
- }
- }
- return;
- }
- void solve() {
- cin >> n;
- G.resize(n);
- p.resize(n);
- d.resize(n);
- b.resize(n);
- vector<bool> used(n, 0);
- REP(i, n) {
- d[i] = 0;
- b[i] = -1;
- G[i].clear();
- }
- p[0] = -1;
- FOR(i, 1, n) {
- cin >> p[i];
- p[i]--;
- G[i].pb(p[i]);
- G[p[i]].pb(i);
- }
- dfs(0);
- priority_queue<pll> q;
- REP(i, n) {
- q.push(mp(d[i], i));
- }
- vl ans;
- ll curr = 0;
- REP(i, n) {
- while (!q.empty() && used[q.top().s]) {
- q.pop();
- }
- if (q.empty()) {
- ans.pb(n);
- continue;
- }
- auto [depth, node] = q.top();
- q.pop();
- curr += depth + 1;
- used[node] = 1;
- while (b[node] != -1) {
- node = b[node];
- used[node] = 1;
- }
- ans.pb(curr);
- }
- print_arr(ans, n, " ");
- return;
- }
- int main() {
- quick;
- //cin >> ttt;
- while (ttt--) solve();
- return 0;
- }