#include <bits/stdc++.h>
using namespace std;
int read() {
  int f, x, c;
  while (!isdigit(c = getchar()) && c != '-')
    ;
  c == '-' ? (f = 1, x = 0) : (f = 0, x = c - '0');
  while (isdigit(c = getchar())) x = (x << 3) + (x << 1) + c - '0';
  return f ? -x : x;
}
struct edge {
  int nxt, to;
} e[500010 << 1];
struct sttck {
  int i, num;
} le[500010], ri[500010];
int n, rt, cnt, head[500010], top1, top2;
vector<int> dp[500010];
void add(int x, int y) {
  e[++cnt].to = y;
  e[cnt].nxt = head[x];
  head[x] = cnt;
}
int dep[500010], top[500010], fa[500010], son[500010], siz[500010], ord[500010],
    mx;
void dfs1(int x, int f) {
  siz[x] = 1;
  dep[x] = dep[f] + 1;
  fa[x] = f;
  dp[dep[x]].push_back(x);
  mx = max(mx, dep[x]);
  for (int i = head[x]; i; i = e[i].nxt) {
    int to = e[i].to;
    if (to == f) continue;
    dfs1(to, x);
    siz[x] += siz[to];
    if (!son[x] || siz[to] > siz[son[x]]) son[x] = to;
  }
}
void dfs2(int x, int r) {
  top[x] = r;
  if (!son[x]) return;
  dfs2(son[x], r);
  for (int i = head[x]; i; i = e[i].nxt) {
    int to = e[i].to;
    if (to != fa[x] && to != son[x]) dfs2(to, to);
  }
}
int lca(int x, int y) {
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) swap(x, y);
    x = fa[top[x]];
  }
  return dep[x] < dep[y] ? x : y;
}
bool cmp(int x, int y) { return dep[x] < dep[y]; }
long long f[500010], g[500010], ans[500010], ansl, ansr;
int main() {
  n = read();
  for (int ff, i = 1; i <= n; i++) {
    ff = read();
    if (!ff)
      rt = i;
    else
      add(ff, i), add(i, ff);
  }
  dfs1(rt, 0);
  dfs2(rt, rt);
  for (int s = 1; s <= mx; s++) {
    int len = dp[s].size() - 1;
    top1 = ansl = top2 = ansr = 0;
    for (int i = 0; i <= len; i++) {
      int x = dp[s][i];
      if (top1) {
        int LCA = lca(x, le[top1].i), num = 0;
        while (top1 > 0 && dep[LCA] <= dep[le[top1].i]) {
          ansl -= 1ll * dep[le[top1].i] * le[top1].num;
          num += le[top1].num;
          top1--;
        }
        ansl += 1ll * dep[LCA] * num;
        le[++top1] = (sttck){LCA, num};
      }
      f[x] = ansl;
      le[++top1] = (sttck){x, 1};
      ansl += dep[x];
    }
    for (int i = len; i >= 0; i--) {
      int x = dp[s][i];
      if (top2) {
        int LCA = lca(x, ri[top2].i), num = 0;
        while (top2 > 0 && dep[LCA] <= dep[ri[top2].i]) {
          ansr -= 1ll * dep[ri[top2].i] * ri[top2].num;
          num += ri[top2].num;
          top2--;
        }
        ansr += 1ll * dep[LCA] * num;
        ri[++top2] = (sttck){LCA, num};
      }
      g[x] = ansr;
      ri[++top2] = (sttck){x, 1};
      ansr += dep[x];
    }
    for (int i = 0; i <= len; i++) {
      int x = dp[s][i];
      ans[x] = f[x] + g[x] + ans[fa[x]] + dep[x];
    }
  }
  for (int i = 1; i <= n; i++) printf("%lld ", ans[i] - dep[i]);
  return 0;
}
