#include <bits/stdc++.h>
using namespace std;
int n, rt, fa[500010], d[500010];
int tot, son[500010], id[500010], len[500010], b[500010];
int a[500010], l[500010], r[500010];
long long ans[500010], s[500010];
int cnt, head[500010], to[1000010], nxt[1000010];
void add(int x, int y) {
  cnt++;
  to[cnt] = y;
  nxt[cnt] = head[x];
  head[x] = cnt;
}
void dfs1(int x) {
  len[x] = 1;
  for (int i = head[x]; i; i = nxt[i]) {
    int y = to[i];
    d[y] = d[x] + 1;
    dfs1(y);
    if (len[y] + 1 > len[x]) {
      son[x] = y;
      len[x] = len[y] + 1;
    }
  }
}
void dfs2(int x) {
  tot++;
  id[x] = tot;
  b[x] = 1;
  if (!son[x]) {
    return;
  }
  dfs2(son[x]);
  for (int i = head[x]; i; i = nxt[i]) {
    int y = to[i];
    if (y == son[x]) {
      continue;
    }
    dfs2(y);
  }
}
void bfs() {
  queue<int> q;
  while (!q.empty()) {
    q.pop();
  }
  tot = 0;
  q.push(rt);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    tot++;
    a[x] = tot;
    l[id[x]] = r[id[x]] = tot;
    if (!son[x]) {
      continue;
    }
    q.push(son[x]);
    for (int i = head[x]; i; i = nxt[i]) {
      int y = to[i];
      if (y == son[x]) {
        continue;
      }
      q.push(y);
    }
  }
}
void merge(int x) {
  if (!son[x]) {
    return;
  }
  merge(son[x]);
  for (int i = head[x]; i; i = nxt[i]) {
    int y = to[i];
    if (y == son[x]) {
      continue;
    }
    merge(y);
    for (int j = 1; j <= len[y]; j++) {
      s[l[id[x] + j]] += 1LL * b[id[y] + j - 1] * d[x];
      s[r[id[x] + j] + 1] -= 1LL * b[id[y] + j - 1] * d[x];
      s[l[id[y] + j - 1]] += 1LL * b[id[x] + j] * d[x];
      s[r[id[y] + j - 1] + 1] -= 1LL * b[id[x] + j] * d[x];
      b[id[x] + j] += b[id[y] + j - 1];
      r[id[x] + j] = r[id[y] + j - 1];
    }
  }
}
void work(int x) {
  for (int i = head[x]; i; i = nxt[i]) {
    int y = to[i];
    ans[y] += ans[x];
    work(y);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &fa[i]);
    add(fa[i], i);
    if (!fa[i]) {
      rt = i;
    }
  }
  d[rt] = 1;
  dfs1(rt);
  dfs2(rt);
  bfs();
  merge(rt);
  for (int i = 1; i <= n; i++) {
    s[i] += s[i - 1];
  }
  for (int i = 1; i <= n; i++) {
    ans[i] = s[a[i]] + d[i] - 1;
  }
  work(rt);
  for (int i = 1; i <= n; i++) {
    printf("%I64d ", ans[i]);
  }
  return 0;
}
