#include <bits/stdc++.h>
using namespace std;
struct P {
  long long first, second, sum;
  P(long long first = 0, long long second = 0, long long sum = 0)
      : first(first), second(second), sum(sum) {}
  bool operator<(const P &b) const { return first < b.first; }
};
const long long MAX_V = 500100;
const long long MAX_LOG_V = 25;
vector<long long> G[MAX_V];
long long root, V;
long long parent[MAX_LOG_V][MAX_V];
long long depth[MAX_V], ans[MAX_V];
struct vertex {
  long long v, di;
  vertex(long long v, long long di = 0) : v(v), di(di) {}
  bool operator<(const vertex &b) const { return di < b.di; }
};
vector<vertex> depVec[MAX_V];
long long dfs_index = 0;
void dfs(long long v, long long p, long long d) {
  parent[0][v] = p;
  depth[v] = d;
  depVec[d].push_back(vertex(v, ++dfs_index));
  for (long long i = 0; i < G[v].size(); i++)
    if (G[v][i] != p) dfs(G[v][i], v, d + 1);
}
void init() {
  dfs(root, -1, 1);
  for (long long k = 0; k + 1 < MAX_LOG_V; k++)
    for (long long v = 1; v <= V; v++) {
      if (parent[k][v] < 0)
        parent[k + 1][v] = -1;
      else
        parent[k + 1][v] = parent[k][parent[k][v]];
    }
}
long long lca(long long u, long long v) {
  if (depth[u] > depth[v]) swap(u, v);
  for (long long k = 0; k < MAX_LOG_V; k++) {
    if ((depth[v] - depth[u]) >> k & 1) v = parent[k][v];
    if (depth[v] == depth[u]) break;
  }
  if (u == v) return u;
  for (long long k = MAX_LOG_V - 1; k >= 0; k--) {
    if (parent[k][u] != parent[k][v]) {
      u = parent[k][u];
      v = parent[k][v];
    }
  }
  return parent[0][u];
}
void solve(const vector<vertex> &vec) {
  vector<P> stk(vec.size() + 1, P(0));
  long long sn = 0;
  long long d = depth[lca(vec[0].v, vec[1].v)];
  stk[sn++] = P(d, 1, d);
  ans[vec[1].v] += stk[0].sum;
  for (long long i = 2; i < vec.size(); i++) {
    long long d = depth[lca(vec[i - 1].v, vec[i].v)];
    if (d < 0) {
      cout << "d = " << d << endl;
    }
    long long x =
        lower_bound(stk.begin(), stk.begin() + sn, P(d, -1)) - stk.begin();
    if (x == sn) {
      stk[sn] = P(d, stk[sn - 1].second + 1, stk[sn - 1].sum + d);
      sn++;
    } else {
      long long cnt = stk[sn - 1].second - (x > 0 ? stk[x - 1].second : 0) + 1;
      if (cnt < 0) {
        cout << "stk[sn-1].second = " << stk[sn - 1].second << endl;
        cout << "x = " << x << endl;
        cout << "sn = " << sn << endl;
        cout << "~ = " << (x > 0 ? stk[x - 1].second : 0) << endl;
      }
      stk[x] =
          P(d, stk[sn - 1].second + 1, (x > 0 ? stk[x - 1].sum : 0) + d * cnt);
      sn = x + 1;
    }
    ans[vec[i].v] += stk[sn - 1].sum;
    if (stk[sn - 1].sum < 0) {
      cout << "vec[i].v = " << vec[i].v << endl;
      for (long long j = 0; j < sn; j++)
        cout << "stk[" << j << "] = " << stk[sn - 1].first << " "
             << stk[sn - 1].second << " " << stk[sn - 1].sum << endl;
      exit(0);
    }
  }
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cin >> V;
  for (long long i = 1; i <= V; i++) {
    long long p;
    cin >> p;
    if (p == 0)
      root = i;
    else
      G[p].push_back(i);
  }
  init();
  for (long long i = 2; i <= V; i++) {
    if (depVec[i].empty()) break;
    if (depVec[i].size() > 1) {
      sort(depVec[i].begin(), depVec[i].end());
      solve(depVec[i]);
      reverse(depVec[i].begin(), depVec[i].end());
      solve(depVec[i]);
    }
    for (auto j : depVec[i]) {
      ans[j.v] += ans[parent[0][j.v]] + depth[j.v] - 1;
    }
  }
  for (long long i = 1; i <= V; i++) {
    cout << ans[i] << (i == V ? '\n' : ' ');
  }
}
