#include <bits/stdc++.h>
const int maxn = 500010;
int n, r;
struct edge {
  int to;
  edge* next;
} E[1000010], *ne = E, *first[maxn];
void link(int u, int v) {
  *ne = (edge){v, first[u]};
  first[u] = ne++;
}
int fa[maxn], dep[maxn], siz[maxn], son[maxn], top[maxn];
std::vector<int> V[maxn];
void init(int i) {
  siz[i] = 1;
  V[dep[i]].push_back(i);
  for (edge* e = first[i]; e; e = e->next)
    if (e->to != fa[i]) {
      fa[e->to] = i;
      dep[e->to] = dep[i] + 1;
      init(e->to);
      if (siz[e->to] > siz[son[i]]) son[i] = e->to;
      siz[i] += siz[e->to];
    }
}
int dfn[maxn], list[maxn], now;
void hld(int i) {
  list[dfn[i] = ++now] = i;
  if (son[i]) top[son[i]] = top[i], hld(son[i]);
  for (edge* e = first[i]; e; e = e->next)
    if (e->to != fa[i] && e->to != son[i]) {
      top[e->to] = e->to;
      hld(e->to);
    }
}
long long B0[500010], B1[500010];
void add(int i, int x) {
  for (int j = i; j <= n; j += j & -j) B1[j] += x, B0[j] += (1 - i) * x;
}
long long query(int i) {
  long long s0 = 0, s1 = 0;
  for (int j = i; j; j -= j & -j) s0 += B0[j], s1 += B1[j];
  return s0 + s1 * i;
}
void c_add(int i) {
  for (; i; i = fa[top[i]]) add(dfn[top[i]], 1), add(dfn[i] + 1, -1);
}
long long c_query(int i) {
  long long s = 0;
  for (; i; i = fa[top[i]]) s += query(dfn[i]) - query(dfn[top[i]] - 1);
  return s;
}
long long ans[maxn];
int main() {
  scanf("%d", &n);
  for (int i = 1, p; i <= n; i++) {
    scanf("%d", &p);
    if (p)
      link(p, i);
    else
      r = i;
  }
  init(r);
  top[r] = r;
  hld(r);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < V[i].size(); j++) c_add(V[i][j]);
    for (int j = 0; j < V[i].size(); j++) ans[V[i][j]] = c_query(fa[V[i][j]]);
  }
  for (int i = 1; i <= n; i++) printf("%lld%c", ans[i] - dep[i], " \n"[i == n]);
}
