#include <bits/stdc++.h>
using namespace std;
void qmax(int &x, int y) {
  if (x < y) x = y;
}
void qmin(int &x, int y) {
  if (x > y) x = y;
}
inline int read() {
  char s;
  int k = 0, base = 1;
  while ((s = getchar()) != '-' && s != EOF && !(isdigit(s)))
    ;
  if (s == EOF) exit(0);
  if (s == '-') base = -1, s = getchar();
  while (isdigit(s)) k = k * 10 + (s ^ '0'), s = getchar();
  return k * base;
}
inline void write(long long x) {
  static char cnt, num[20];
  cnt = 0;
  if (!x) {
    putchar('0');
    return;
  }
  for (; x; x /= 10) num[++cnt] = x % 10;
  for (; cnt; putchar(num[cnt--] + 48))
    ;
}
const int maxn = 5e5 + 10;
int n;
int to[maxn << 1], ne[maxn << 1], po[maxn], id;
inline void add(int x, int y) {
  id++;
  to[id] = y;
  ne[id] = po[x];
  po[x] = id;
}
int dfs_time, dfn[maxn], son[maxn], fa[maxn], sz[maxn];
int deep[maxn];
int top[maxn], top_id, Map[maxn];
int q, b[maxn];
long long t[2][maxn];
void xg(int x, long long y) {
  int xx = x;
  while (x <= n) {
    t[0][x] += y;
    t[1][x] += y * xx;
    x += x & -x;
  }
}
long long qsum(int x) {
  long long sum = 0;
  long long xx = x;
  while (x) {
    sum += t[0][x] * (xx + 1) - t[1][x];
    x -= x & -x;
  }
  return sum;
}
void dfs1(int x) {
  int y;
  sz[x] = 1;
  for (int i = po[x]; i; i = ne[i]) {
    y = to[i];
    deep[y] = deep[x] + 1;
    dfs1(y);
    sz[x] += sz[y];
    if (sz[y] > sz[son[x]]) son[x] = y;
  }
}
void dfs(int x, int last) {
  dfn[x] = ++dfs_time;
  Map[dfs_time] = x;
  top[x] = last;
  if (son[x] == 0) return;
  dfs(son[x], last);
  for (int i = po[x]; i; i = ne[i])
    if (to[i] != son[x] && to[i] != fa[x]) dfs(to[i], to[i]);
}
int rt;
int lca(int x, int y) {
  int tx = top[x], ty = top[y];
  while (tx != ty) {
    if (deep[tx] < deep[ty]) swap(tx, ty), swap(x, y);
    x = fa[tx];
    tx = top[x];
  }
  if (deep[x] < deep[y]) return x;
  return y;
}
void xg(int x) {
  x = fa[x];
  if (!x) return;
  int y = rt;
  int tx = top[x], ty = top[rt];
  while (tx != ty) {
    xg(dfn[top[x]], 1);
    xg(dfn[x] + 1, -1);
    x = fa[tx], tx = top[x];
  }
  xg(dfn[top[x]], 1);
  xg(dfn[x] + 1, -1);
}
long long ans[maxn];
void qans(int x) {
  int tx = top[x];
  int xx = x;
  int y = rt, ty = top[y];
  while (tx != ty) {
    ans[xx] += qsum(dfn[x]) - qsum(dfn[tx] - 1);
    x = fa[tx], tx = top[x];
  }
  ans[xx] += qsum(dfn[x]) - qsum(dfn[tx] - 1);
}
bool cmp(int aa, int bb) { return deep[aa] < deep[bb]; }
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    fa[i] = read();
    if (fa[i]) {
      add(fa[i], i);
    } else {
      rt = i;
    }
  }
  deep[0] = -1;
  dfs1(rt);
  dfs(rt, rt);
  for (int i = 1; i <= n; i++) b[i] = i;
  sort(b + 1, b + n + 1, cmp);
  for (int i = 1, l, r; i <= n;) {
    l = i;
    r = i;
    while (r < n && deep[b[r + 1]] == deep[b[r]]) r++;
    for (int j = l; j <= r; j++) {
      xg(b[j]);
    }
    for (int j = l; j <= r; j++) {
      qans(b[j]);
    }
    i = r + 1;
  }
  for (int i = 1; i <= n; i++) {
    printf("%lld ", ans[i]);
  }
  return 0;
}
