#include <bits/stdc++.h>
const int MAXN = 5e5 + 5;
const int MAXM = 19;
struct Edge {
  int to, nxt;
} e[MAXN << 1];
int n;
int head[MAXN], f[MAXN][MAXM + 1], dep[MAXN], dfn[MAXN], cnt;
std::vector<int> G[MAXN];
inline void add(int u, int v) {
  e[++cnt] = (Edge){v, head[u]};
  head[u] = cnt;
  e[++cnt] = (Edge){u, head[v]};
  head[v] = cnt;
}
inline void dfs(int v, int fa = 0) {
  static int ts = 0;
  dfn[v] = ++ts;
  for (int i = 1; i <= MAXM; ++i) f[v][i] = f[f[v][i - 1]][i - 1];
  G[dep[v]].push_back(v);
  for (int i = head[v]; i; i = e[i].nxt) {
    if (e[i].to == fa) continue;
    dep[e[i].to] = dep[v] + 1;
    f[e[i].to][0] = v;
    dfs(e[i].to, v);
  }
}
inline int lca(int x, int y) {
  if (dep[x] != dep[y]) {
    if (dep[x] < dep[y]) std::swap(x, y), std::swap(dep[x], dep[y]);
    int d = dep[x] - dep[y];
    for (int i = 0; i <= MAXM; ++i)
      if ((1 << i) & d) x = f[x][i];
  }
  if (x == y) return x;
  for (int i = MAXM; i >= 0; --i) {
    if (f[x][i] == f[y][i]) continue;
    x = f[x][i], y = f[y][i];
  }
  return f[x][0];
}
long long ans[MAXN];
int a[MAXN], b[MAXN], c[MAXN], top;
inline void Solve(std::vector<int> &v) {
  top = 0;
  long long res = 0;
  for (int i = 0; i <= (int)v.size() - 1; ++i) {
    int x = v[i];
    if (!i)
      a[++top] = x, b[top] = 0, c[top] = 0;
    else {
      while ("Sqytxdy") {
        int l = lca(a[top], x);
        if (b[top] <= dep[l]) {
          a[++top] = x;
          b[top] = dep[l];
          c[top] = i;
          break;
        }
        res -= 1ll * (c[top] - c[top - 1]) * b[top];
        --top;
      }
      res += 1ll * (c[top] - c[top - 1]) * b[top];
    }
    ans[x] += res;
  }
}
int main() {
  scanf("%d", &n);
  int root = 0;
  for (int i = 1; i <= n; ++i) {
    int f;
    scanf("%d", &f);
    if (f)
      add(f, i);
    else
      root = i;
  }
  dep[root] = 1;
  dfs(root);
  for (int d = 1; d <= n; ++d) {
    for (auto x : G[d]) ans[x] += ans[f[x][0]] + d;
    Solve(G[d]);
    std::reverse(G[d].begin(), G[d].end());
    Solve(G[d]);
  }
  for (int i = 1; i <= n; ++i) printf("%lld ", ans[i] - dep[i]);
  puts("");
  return 0;
}
