#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int fa[N], dep[N], siz[N], son[N];
int top[N], id[N];
long long tr[N], tri[N];
int tot;
struct edge {
  int t, nxt;
} e[N];
int head[N], cnt;
int n;
void add(int u, int v) {
  e[++cnt].t = v;
  e[cnt].nxt = head[u];
  head[u] = cnt;
}
vector<int> v[N];
long long ans[N];
void dfs1(int x) {
  siz[x] = 1;
  dep[x] = dep[fa[x]] + 1;
  v[dep[x]].push_back(x);
  for (int i = head[x]; i; i = e[i].nxt) {
    int v = e[i].t;
    dfs1(v);
    if (siz[v] > siz[son[x]]) son[x] = v;
    siz[x] += siz[v];
  }
}
void dfs2(int x, int tp) {
  top[x] = tp;
  id[x] = ++tot;
  if (son[x]) dfs2(son[x], tp);
  for (int i = head[x]; i; i = e[i].nxt) {
    int v = e[i].t;
    if (v == son[x]) continue;
    dfs2(v, v);
  }
}
inline void Add(int x, int d) {
  int v = x;
  while (x <= n) {
    tr[x] += d;
    tri[x] += v * d;
    x += x & -x;
  }
}
inline long long query(int x) {
  long long ans = 0;
  int v = x;
  while (x) {
    ans += (v + 1) * tr[x];
    ans -= tri[x];
    x -= x & -x;
  }
  return ans;
}
int rt;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &fa[i]);
    if (fa[i] == 0)
      rt = i;
    else
      add(fa[i], i);
  }
  dfs1(rt);
  dfs2(rt, rt);
  for (int i = 2; !v[i].empty(); ++i) {
    vector<int>::iterator j;
    for (j = v[i].begin(); j != v[i].end(); ++j) {
      for (int k = fa[*j]; k; k = fa[top[k]]) {
        Add(id[top[k]], 1);
        Add(id[k] + 1, -1);
      }
    }
    for (j = v[i].begin(); j != v[i].end(); ++j) {
      for (int k = fa[*j]; k; k = fa[top[k]]) {
        ans[*j] += query(id[k]) - query(id[top[k]] - 1);
      }
    }
  }
  for (int i = 1; i <= n; ++i) printf("%lld ", ans[i]);
}
