#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 50;
int n, rt;
struct E {
  int d;
  long long w;
};
vector<E> g1[N], g2[N];
int dep[N], len[N], ps[N];
void dfs(int v) {
  len[v] = 1;
  ps[v] = 0;
  for (E e : g1[v]) {
    dep[e.d] = dep[v] + 1;
    dfs(e.d);
    if (len[e.d] + 1 > len[v]) {
      len[v] = len[e.d] + 1;
      ps[v] = e.d;
    }
  }
}
struct S {
  int v;
  long long cnt;
};
S pol[N << 1], *f[N], *top = pol;
long long ans[N];
void dfs2(int v) {
  f[v] = top++;
  f[v][1] = (S){v, 1};
  if (ps[v]) {
    dfs2(ps[v]);
  }
  for (E e : g1[v]) {
    if (e.d == ps[v]) continue;
    dfs2(e.d);
    for (int j = 1; j <= (len[e.d]); ++j) {
      S &a = f[v][j + 1], b = f[e.d][j];
      ;
      ;
      ans[a.v] += b.cnt * dep[v];
      ans[b.v] += a.cnt * dep[v];
      g2[a.v].push_back((E){b.v, -ans[a.v] + ans[b.v]});
      ;
      a.cnt += b.cnt;
    }
  }
}
void dfs3(int v) {
  for (E e : g2[v]) {
    ans[e.d] = ans[v] + e.w;
    dfs3(e.d);
  }
}
void dfs4(int v) {
  for (E e : g1[v]) {
    ans[e.d] += dep[e.d] - 1 + ans[v];
    dfs4(e.d);
  }
}
int main(int argc, char *argv[]) {
  scanf("%d", &n);
  for (int i = 1; i <= (n); ++i) {
    int p;
    scanf("%d", &p);
    if (!p)
      rt = i;
    else
      g1[p].push_back((E){i, 0});
  }
  dep[rt] = 1;
  dfs(rt);
  dfs2(rt);
  ;
  for (int i = 1; i <= (n); ++i)
    ;
  for (int j = 1; j <= (len[rt]); ++j) {
    ;
    dfs3(f[rt][j].v);
  };
  for (int i = 1; i <= (n); ++i)
    ;
  dfs4(rt);
  for (int i = 1; i <= (n); ++i) {
    printf("%lld\n", ans[i]);
  }
  return 0;
}
