#include <bits/stdc++.h>
using namespace std;
inline char gc() {
  static char now[1 << 16], *S, *T;
  if (S == T) {
    T = (S = now) + fread(now, 1, 1 << 16, stdin);
    if (S == T) return EOF;
  }
  return *S++;
}
inline int read() {
  int x = 0, f = 1;
  char c = gc();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = gc();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + c - 48;
    c = gc();
  }
  return x * f;
}
void print(long long x) {
  if (x / 10) print(x / 10);
  putchar('0' + x % 10);
}
int n, fa[500010], rt;
struct adj {
  int to, nxt;
} e[500010 << 1];
int hd[500010], cnt = 0;
inline void addedge(int x, int y) {
  e[++cnt] = (adj){y, hd[x]};
  hd[x] = cnt;
}
int dep[500010], son[500010], sz[500010], dfn[500010], rl[500010], tim = 0;
void dfs1(int x) {
  sz[x] = 1;
  son[x] = 0;
  for (int i = hd[x]; i; i = e[i].nxt) {
    int y = e[i].to;
    dep[y] = dep[x] + 1;
    dfs1(y);
    sz[x] += sz[y];
    if (!son[x] || sz[son[x]] < sz[y]) son[x] = y;
  }
}
vector<int> c[500010];
vector<int>::iterator it;
inline bool cmp(int x, int y) { return dep[x] < dep[y]; }
int tp[500010];
void dfs2(int x, int Top) {
  tp[x] = Top;
  dfn[x] = ++tim;
  rl[tim] = x;
  if (son[x]) dfs2(son[x], Top);
  for (int i = hd[x]; i; i = e[i].nxt) {
    int y = e[i].to;
    if (y != son[x]) dfs2(y, y);
  }
}
long long ans[500010], tag[500010 << 2], sum[500010 << 2];
void add(int p, int l, int r, int ql, int qr) {
  sum[p] += qr - ql + 1;
  if (ql == l && r == qr) {
    ++tag[p];
    return;
  }
  int mid = (l + r) >> 1;
  if (qr <= mid)
    add(p << 1, l, mid, ql, qr);
  else if (mid < ql)
    add(p << 1 | 1, mid + 1, r, ql, qr);
  else
    add(p << 1, l, mid, ql, mid), add(p << 1 | 1, mid + 1, r, mid + 1, qr);
}
inline void add_path(int x) {
  while (true) {
    add(1, 1, n, dfn[tp[x]], dfn[x]);
    if (tp[x] == rt) return;
    x = fa[tp[x]];
  }
}
long long query(int p, int l, int r, int ql, int qr) {
  if (ql == l && r == qr) return sum[p];
  int mid = (l + r) >> 1;
  if (qr <= mid)
    return query(p << 1, l, mid, ql, qr) + tag[p] * (qr - ql + 1);
  else if (mid < ql)
    return query(p << 1 | 1, mid + 1, r, ql, qr) + tag[p] * (qr - ql + 1);
  else
    return query(p << 1, l, mid, ql, mid) +
           query(p << 1 | 1, mid + 1, r, mid + 1, qr) + tag[p] * (qr - ql + 1);
}
inline long long query_path(int x) {
  long long ret = 0;
  while (true) {
    ret += query(1, 1, n, dfn[tp[x]], dfn[x]);
    if (tp[x] == rt) return ret;
    x = fa[tp[x]];
  }
}
int main() {
  n = read();
  for (int i = 1; i <= n; ++i) {
    fa[i] = read();
    if (!fa[i])
      rt = i;
    else
      addedge(fa[i], i);
  }
  dep[rt] = 1;
  dfs1(rt);
  dfs2(rt, rt);
  for (int i = 1; i <= n; ++i) c[dep[i]].push_back(i);
  ans[rt] = 0;
  for (int i = 2; i <= n; ++i) {
    for (it = c[i].begin(); it != c[i].end(); ++it) {
      add_path(fa[*it]);
    }
    for (it = c[i].begin(); it != c[i].end(); ++it) {
      ans[*it] = query_path(fa[*it]);
    }
  }
  for (int i = 1; i <= n; ++i) print(ans[i]), putchar(' ');
  return 0;
}
