#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5 + 5;
char buf[1 << 12], *pp1 = buf, *pp2 = buf, nc;
int ny;
inline char gc() {
  return pp1 == pp2 &&
                 (pp2 = (pp1 = buf) + fread(buf, 1, 1 << 12, stdin), pp1 == pp2)
             ? EOF
             : *pp1++;
}
inline int read() {
  int x = 0;
  for (ny = 1; nc = gc(), (nc < 48 || nc > 57) && nc != EOF;)
    if (nc == 45) ny = -1;
  if (nc < 0) return nc;
  for (x = nc - 48; nc = gc(), 47 < nc && nc < 58 && nc != EOF;
       x = (x << 3) + (x << 1) + (nc ^ 48))
    ;
  return x * ny;
}
int n, tim, dfn[MAXN], tr[MAXN], top[MAXN], d[MAXN], w[MAXN], fa[MAXN],
    sz[MAXN], rt;
long long ans[MAXN];
vector<int> G[MAXN], p[MAXN];
long long val[MAXN], val2[MAXN];
inline int lowbit(int x) { return x & -x; }
inline void add(int pos, long long v) {
  for (int x = pos; x <= n; x += lowbit(x)) val[x] += v, val2[x] += v * pos;
}
inline void Add(int l, int r, long long v) { add(l, v), add(r + 1, -v); }
inline long long ask(int x) {
  long long ans = 0;
  for (; x; x -= lowbit(x)) ans += val[x];
  return ans;
}
inline long long ask2(int x) {
  long long ans = 0;
  for (; x; x -= lowbit(x)) ans += val2[x];
  return ans;
}
inline long long Ask(int l, int r) {
  return (ask(r) * (r + 1) - ask2(r)) - (ask(l - 1) * l - ask2(l - 1));
}
void dfs(int x, int f) {
  fa[x] = f, d[x] = d[f] + 1, sz[x] = 1, p[d[x]].push_back(x);
  for (auto y : G[x])
    dfs(y, x), sz[x] += sz[y], sz[y] > sz[w[x]] ? w[x] = y : 0;
}
void dfs1(int x, int tp) {
  top[x] = tp, tr[dfn[x] = ++tim] = x;
  if (w[x]) dfs1(w[x], tp);
  for (auto y : G[x])
    if (y != w[x]) dfs1(y, y);
}
inline void Add(int x) {
  for (; x; x = fa[top[x]]) Add(dfn[top[x]], dfn[x], 1);
}
inline long long Ask(int x) {
  long long ans = 0;
  for (; x; x = fa[top[x]]) ans += Ask(dfn[top[x]], dfn[x]);
  return ans;
}
int main() {
  n = read();
  for (int i = (1); i <= (n); i++) {
    int x = read();
    if (x)
      G[x].push_back(i);
    else
      rt = i;
  }
  dfs(rt, 0), dfs1(rt, rt);
  for (int i = (1); i <= (n); i++) {
    for (auto o : p[i]) Add(o);
    for (auto o : p[i]) ans[o] = Ask(o) - i;
  }
  for (int i = (1); i <= (n); i++) cout << ans[i] << " ";
  cout << "\n";
  return 0;
}
