#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500005;
vector<int> G[MAXN];
vector<int> id[MAXN];
int n, cnt, root;
long long ans[MAXN];
int fa[MAXN], size[MAXN], deep[MAXN];
int son[MAXN], dfn[MAXN], top[MAXN];
namespace BIT {
long long c0[MAXN], c1[MAXN];
void add(long long *c, int x, long long k) {
  for (; x <= n; x += x & -x) c[x] += k;
}
long long sum(long long *c, int x) {
  long long r = 0;
  for (; x; x -= x & -x) r += c[x];
  return r;
}
void add(int l, int r, long long k) {
  if (l > r) swap(l, r);
  add(c0, l, k);
  add(c0, r + 1, -k);
  add(c1, l, (l - 1) * k);
  add(c1, r + 1, -r * k);
}
long long sum(int l, int r) {
  if (l > r) swap(l, r);
  return sum(c0, r) * r - sum(c1, r) - sum(c0, l - 1) * (l - 1) +
         sum(c1, l - 1);
}
}  // namespace BIT
void dfs1(int cur, int father, int dep) {
  fa[cur] = father;
  deep[cur] = dep;
  size[cur] = 1;
  son[cur] = 0;
  for (int i = 0; i < G[cur].size(); i++) {
    int nx = G[cur][i];
    if (nx == fa[cur]) continue;
    dfs1(nx, cur, dep + 1);
    size[cur] += size[nx];
    if (size[son[cur]] < size[nx]) {
      son[cur] = nx;
    }
  }
}
void dfs2(int cur, int tp) {
  top[cur] = tp;
  dfn[cur] = ++cnt;
  if (!son[cur]) return;
  dfs2(son[cur], tp);
  for (int i = 0; i < G[cur].size(); i++) {
    int nx = G[cur][i];
    if (nx == fa[cur] || nx == son[cur]) {
      continue;
    }
    dfs2(nx, nx);
  }
}
void add(int u, int v, int w) {
  int t1 = top[u], t2 = top[v];
  while (t1 != t2) {
    if (deep[t1] < deep[t2]) {
      swap(t1, t2);
      swap(u, v);
    }
    BIT::add(dfn[u], dfn[t1], w);
    u = fa[t1], t1 = top[u];
  }
  BIT::add(dfn[u], dfn[v], w);
}
long long query(int u, int v) {
  int t1 = top[u], t2 = top[v];
  long long res = 0;
  while (t1 != t2) {
    if (deep[t1] < deep[t2]) {
      swap(t1, t2);
      swap(u, v);
    }
    res += BIT::sum(dfn[u], dfn[t1]);
    u = fa[t1], t1 = top[u];
  }
  res += BIT::sum(dfn[u], dfn[v]);
  return res;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int p;
    scanf("%d", &p);
    if (p) {
      G[p].push_back(i);
      G[i].push_back(p);
    } else {
      root = i;
    }
  }
  dfs1(root, 0, 1);
  dfs2(root, root);
  for (int i = 1; i <= n; i++) {
    id[deep[i]].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    for (auto cur : id[i]) {
      add(cur, root, 1);
    }
    for (auto cur : id[i]) {
      ans[cur] = query(cur, root) - deep[cur];
    }
  }
  for (int i = 1; i <= n; i++) {
    printf("%lld ", ans[i]);
  }
  putchar('\n');
  return 0;
}
