#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10, TOP = 20;
struct xxx {
  int y, nxt;
} a[N * 2];
int h[N], fa[N], dep[N], dfn[N], lv[N], rv[N];
int f[N][TOP + 1];
int lis[N], st[N], rec_dep[N], rec_id[N];
long long ans[N];
int n, m, tot, dfn_t, rt;
bool cmp(int x, int y) {
  return dep[x] == dep[y] ? dfn[x] < dfn[y] : dep[x] < dep[y];
}
void add(int x, int y) {
  a[++tot].y = y;
  a[tot].nxt = h[x];
  h[x] = tot;
}
void dfs(int x) {
  int u;
  dep[x] = dep[fa[x]] + 1;
  f[x][0] = fa[x];
  dfn[x] = ++dfn_t;
  for (int i = 1; i <= TOP; ++i) f[x][i] = f[f[x][i - 1]][i - 1];
  for (int i = h[x]; i != -1; i = a[i].nxt) {
    u = a[i].y;
    dfs(u);
  }
}
int get_lca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  for (int i = TOP; i >= 0; --i)
    if (dep[f[x][i]] >= dep[y]) x = f[x][i];
  if (x == y) return x;
  for (int i = TOP; i >= 0; --i)
    if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
  return f[x][0];
}
void calc(int l, int r) {
  int x, lca, p;
  long long sum;
  for (int i = l; i <= r; ++i) {
    x = lis[i];
    lv[x] = rv[x] = 0;
    ans[x] = ans[fa[x]] + dep[fa[x]];
  }
  p = 0;
  sum = 0;
  lca = 0;
  for (int i = l; i <= r; ++i) {
    x = lis[i];
    while (p && rec_dep[p] >= dep[lca = get_lca(x, st[p])])
      sum -= 1LL * (rec_id[p] - rec_id[p - 1]) * rec_dep[p], --p;
    st[++p] = x;
    rec_dep[p] = lca ? dep[lca] : 0;
    rec_id[p] = i;
    sum += 1LL * (rec_id[p] - rec_id[p - 1]) * rec_dep[p];
    ans[x] += sum;
  }
  p = 0;
  sum = 0;
  lca = 0;
  for (int i = r; i >= l; --i) {
    x = lis[i];
    while (p && rec_dep[p] >= dep[lca = get_lca(x, st[p])])
      sum -= 1LL * (rec_id[p - 1] - rec_id[p]) * rec_dep[p], --p;
    st[++p] = x;
    rec_dep[p] = dep[lca];
    rec_id[p] = i;
    sum += 1LL * (rec_id[p - 1] - rec_id[p]) * rec_dep[p];
    ans[x] += sum;
  }
}
void solve() {
  for (int i = 1; i <= n; ++i) lis[i] = i;
  sort(lis + 1, lis + 1 + n, cmp);
  int p1 = 1, p2 = 1;
  for (int i = 1; i <= n; i = p2 + 1) {
    p1 = p2 = i;
    while (dep[lis[p2 + 1]] == dep[lis[p1]]) ++p2;
    calc(p1, p2);
  }
}
int main() {
  scanf("%d", &n);
  memset(h, -1, sizeof(h));
  tot = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", fa + i);
    if (fa[i])
      add(fa[i], i);
    else
      rt = i;
  }
  dep[rt] = 1;
  dfs(rt);
  solve();
  for (int i = 1; i <= n; ++i) printf("%I64d ", ans[i]);
  printf("\n");
}
