#include <bits/stdc++.h>
using namespace std;
int rt, n, i, j, id, tot, p[555555], fir[555555], ne[555555], la[555555],
    st[555555], bl[555555], hs[555555], F[555555], h[555555], sz[555555];
vector<int> V[555555];
long long an[555555];
struct BIT {
  long long a[555555], b[555555];
  void ad(int x, int z) {
    for (int i = x; i <= n; i += i & -i) a[i] += z, b[i] += z * (x - 1);
  }
  long long qu(long long x) {
    long long v0 = 0, v1 = 0;
    for (int i = x; i; i -= i & -i) v0 += a[i], v1 += b[i];
    return x * v0 - v1;
  }
  void add(int x, int y, int z) {
    ad(x, z);
    ad(y + 1, -z);
  }
  long long sum(int x, int y) { return qu(y) - qu(x - 1); }
} B;
void ins(int x, int y) {
  la[++tot] = y;
  ne[tot] = fir[x];
  fir[x] = tot;
}
void dfs(int x) {
  sz[x] = 1;
  V[h[x]].push_back(x);
  for (int i = fir[x], y; i; i = ne[i])
    if (y = la[i], F[x] != y) {
      F[y] = x;
      h[y] = h[x] + 1;
      dfs(y);
      sz[x] += sz[y];
      if (sz[y] > sz[hs[x]]) hs[x] = y;
    }
}
void dfs2(int x, int f) {
  st[x] = ++id;
  bl[x] = f;
  if (hs[x]) dfs2(hs[x], f);
  for (int i = fir[x], y; i; i = ne[i])
    if (y = la[i], F[x] != y && y != hs[x]) dfs2(y, y);
}
void add(int x) {
  for (; bl[x] != bl[rt]; x = F[bl[x]]) B.add(st[bl[x]], st[x], 1);
  B.add(1, st[x], 1);
}
long long qu(int x) {
  long long V = 0;
  for (; bl[x] != bl[rt]; x = F[bl[x]]) V += B.sum(st[bl[x]], st[x]);
  V += B.sum(1, st[x]);
  return V;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    if (scanf("%d", &p[i]), p[i])
      ins(p[i], i);
    else
      rt = i;
  dfs(rt);
  dfs2(rt, rt);
  for (int i = 1; i <= n; i++) {
    for (j = 0; j < V[i].size(); j++) add(F[V[i][j]]);
    for (j = 0; j < V[i].size(); j++) an[V[i][j]] = qu(F[V[i][j]]);
  }
  for (int i = 1; i <= n; i++) printf("%I64d%c", an[i], i == n ? '\n' : ' ');
}
