#include <bits/stdc++.h>
using namespace std;
const int MAXN = 550000;
int fa[MAXN], dep[MAXN];
int siz[MAXN], son[MAXN];
int cnt, dfn[MAXN], fir[MAXN];
int val[MAXN];
long long ans[MAXN];
vector<int> g[MAXN], pos[MAXN];
void dfs1(int u, int d) {
  dep[u] = d, siz[u] = 1, son[u] = 0;
  int mx = 0;
  for (int v : g[u])
    if (v != fa[u]) {
      dfs1(v, d + 1);
      siz[u] += siz[v];
      if (siz[v] > mx) mx = siz[v], son[u] = v;
    }
}
void dfs2(int u, int p) {
  dfn[u] = ++cnt, fir[u] = p;
  val[cnt] = siz[u] - 1;
  if (!son[u]) return;
  dfs2(son[u], p);
  for (int v : g[u])
    if (v != fa[u] && v != son[u]) dfs2(v, v);
}
struct SegmentTree {
  struct Node {
    int l, r;
    long long val, add;
  } tr[4 * MAXN];
  void pushup(int o) { tr[o].val = tr[(o << 1)].val + tr[(o << 1 | 1)].val; }
  void add(int o, long long k) {
    tr[o].val += k * (tr[o].r - tr[o].l + 1);
    tr[o].add += k;
  }
  void pushdown(int o) {
    if (tr[o].add) {
      add((o << 1), tr[o].add);
      add((o << 1 | 1), tr[o].add);
      tr[o].add = 0;
    }
  }
  void build(int o, int l, int r, int* a) {
    tr[o].l = l, tr[o].r = r, tr[o].add = 0;
    if (l == r) {
      tr[o].val = a[l];
      return;
    }
    int mid = l + r >> 1;
    build((o << 1), l, mid, a);
    build((o << 1 | 1), mid + 1, r, a);
    pushup(o);
  }
  void update(int o, int l, int r, int k) {
    if (tr[o].l > r || tr[o].r < l) return;
    if (l <= tr[o].l && tr[o].r <= r) {
      add(o, k);
      return;
    }
    pushdown(o);
    update((o << 1), l, r, k);
    update((o << 1 | 1), l, r, k);
    pushup(o);
  }
  long long query(int o, int l, int r) {
    if (tr[o].l > r || tr[o].r < l) return 0;
    if (l <= tr[o].l && tr[o].r <= r) return tr[o].val;
    pushdown(o);
    return query((o << 1), l, r) + query((o << 1 | 1), l, r);
  }
  void getval(int o, int* a) {
    if (tr[o].l == tr[o].r) {
      a[tr[o].l] = tr[o].val;
      return;
    }
    pushdown(o);
    getval((o << 1), a);
    getval((o << 1 | 1), a);
  }
} sgt;
int main() {
  int n, rt;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &fa[i]);
    if (fa[i])
      g[fa[i]].push_back(i);
    else
      rt = i;
  }
  dfs1(rt, 1);
  dfs2(rt, rt);
  sgt.build(1, 1, n, val);
  for (int i = 1; i <= n; i++) pos[dep[i]].push_back(i);
  for (int i = n; i > 100; i--) {
    for (int u : pos[i]) {
      int v = fa[u];
      while (v) {
        ans[u] += sgt.query(1, dfn[fir[v]], dfn[v]);
        v = fa[fir[v]];
      }
    }
    for (int u : pos[i]) {
      int v = fa[u];
      while (v) {
        sgt.update(1, dfn[fir[v]], dfn[v], -1);
        v = fa[fir[v]];
      }
    }
  }
  sgt.getval(1, val);
  for (int i = min(n, 100); i >= 1; i--) {
    for (int u : pos[i]) {
      int v = fa[u];
      while (v) ans[u] += val[dfn[v]], v = fa[v];
    }
    for (int u : pos[i]) {
      int v = fa[u];
      while (v) val[dfn[v]]--, v = fa[v];
    }
  }
  for (int i = 1; i <= n; i++) printf("%lld ", ans[i]);
  putchar('\n');
  return 0;
}
