#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O2,no-stack-protector,unroll-loops,fast-math")
const long long maxn = 5e5 + 10, maxm = 2e5 + 500, lg = 19, mod = 1e9 + 7,
                inf = 1e18;
long long n, par[maxn][lg + 1], ans[maxn], rt, h[maxn];
vector<long long> g[maxn], rk[maxn];
void dfs(long long v, long long p = 0) {
  rk[h[v] = h[p] + 1].push_back(v);
  for (int i = 1; i <= lg; i++) par[v][i] = par[par[v][i - 1]][i - 1];
  for (auto u : g[v]) dfs(u, v);
}
long long lca(long long v, long long u) {
  if (h[v] < h[u]) swap(v, u);
  for (int i = 0, x = h[v] - h[u]; i <= lg; i++)
    if (x >> i & 1) v = par[v][i];
  if (v == u) return v;
  for (int i = lg; i >= 0; i--)
    if (par[v][i] != par[u][i]) v = par[v][i], u = par[u][i];
  return par[v][0];
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> par[i][0];
    g[par[i][0]].push_back(i);
  }
  for (int i = 1; i <= n; i++)
    if (par[i][0] == 0) {
      dfs(i);
      break;
    }
  for (int r = 1; r < maxn; r++) {
    if (rk[r].empty()) break;
    for (auto v : rk[r]) ans[v] += ans[par[v][0]] - (h[v] - 1);
    for (int T = 0; T < 2; T++) {
      long long nw = 0;
      vector<pair<long long, long long> > vv;
      for (int i = 0; i < (long long)(rk[r].size()); i++) {
        long long u = rk[r][i], ted = 0;
        while (!vv.empty() && h[vv.back().first] >= h[u]) {
          u = lca(u, vv.back().first);
          nw -= h[vv.back().first] * vv.back().second;
          ted += vv.back().second;
          vv.pop_back();
        }
        if (ted) nw += h[u] * ted, vv.push_back({u, ted});
        nw += h[rk[r][i]], vv.push_back({rk[r][i], 1});
        ans[rk[r][i]] += nw - 1;
      }
      reverse(rk[r].begin(), rk[r].end());
    }
  }
  for (int i = 1; i <= n; i++) cout << ans[i] << ' ';
  return 0;
}
