#include <bits/stdc++.h>
using namespace std;
const int maxn = 500010;
int n, root, cnt = 0, md = 0, fa[maxn], head[maxn];
int dep[maxn], sz[maxn], son[maxn], top[maxn];
int a[maxn], c[maxn], d[maxn];
long long ans[maxn];
vector<int> g[maxn];
struct edge {
  int u, v, nxt;
} e[maxn];
void add(int u, int v) {
  e[++cnt] = (edge){u, v, head[u]};
  head[u] = cnt;
}
void dfs1(int u) {
  sz[u] = 1;
  md = max(md, dep[u]);
  g[dep[u]].push_back(u);
  for (int i = head[u]; i; i = e[i].nxt) {
    dep[e[i].v] = dep[u] + 1;
    dfs1(e[i].v);
    sz[u] += sz[e[i].v];
    if (sz[son[u]] < sz[e[i].v]) son[u] = e[i].v;
  }
}
void dfs2(int u, int tp) {
  top[u] = tp;
  if (son[u]) dfs2(son[u], tp);
  for (int i = head[u]; i; i = e[i].nxt)
    if (e[i].v != son[u]) dfs2(e[i].v, e[i].v);
}
int lca(int u, int v) {
  while (top[u] != top[v]) {
    if (dep[top[u]] < dep[top[v]]) swap(u, v);
    u = fa[top[u]];
  }
  return dep[u] < dep[v] ? u : v;
}
void solve(int l) {
  int m = g[l].size() - 1, r = 1;
  long long sum = 0;
  a[1] = g[l][0];
  c[1] = d[1] = 0;
  for (int i = 1; i <= m; i++) {
    int p, u = g[l][i];
    while (true) {
      p = lca(u, a[r]);
      if (d[r] < dep[p] + 1) {
        a[++r] = u;
        c[r] = i;
        d[r] = dep[p] + 1;
        break;
      }
      sum -= 1ll * d[r] * (c[r] - c[r - 1]);
      r--;
    }
    sum += 1ll * d[r] * (c[r] - c[r - 1]);
    ans[u] += sum;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &fa[i]);
    if (!fa[i])
      root = i;
    else
      add(fa[i], i);
  }
  dfs1(root);
  dfs2(root, 0);
  for (int i = 1; i <= md; i++) {
    int m = g[i].size() - 1;
    for (int j = 0; j <= m; j++) ans[g[i][j]] += ans[fa[g[i][j]]] + i;
    solve(i);
    reverse(g[i].begin(), g[i].end());
    solve(i);
  }
  for (int i = 1; i <= n; i++) printf("%I64d ", ans[i]);
  return 0;
}
