#include <bits/stdc++.h>
using namespace std;
long long Ans[500010];
vector<int> g[500010], e[500010];
int i, j, k, n, m, p, l;
int a[500010], b[500010], c[500010];
int x, Rt, d[500010], f[500010][20];
inline void Dfs(int x) {
  g[d[x]].push_back(x);
  for (int i = 0; i < e[x].size(); i++) d[e[x][i]] = d[x] + 1, Dfs(e[x][i]);
}
inline int Lca(int x, int y) {
  if (d[x] < d[y]) swap(x, y);
  for (int i = 20 - 1; i >= 0; i--)
    if (d[f[x][i]] >= d[y]) x = f[x][i];
  if (x == y) return x;
  for (int i = 20 - 1; i >= 0; i--)
    if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
  return f[x][0];
}
inline void Solve() {
  long long sum = 0;
  for (int j = 0; j < g[i].size(); j++) {
    int x = g[i][j];
    if (!j)
      a[l = 1] = x, b[1] = c[1] = 0;
    else {
      while (1) {
        int L = Lca(a[l], x);
        if (b[l] <= d[L]) {
          a[++l] = x;
          b[l] = d[L] + 1;
          c[l] = j;
          break;
        }
        sum -= 1ll * (c[l] - c[l - 1]) * b[l];
        l--;
      }
      sum += 1ll * (c[l] - c[l - 1]) * b[l];
    }
    Ans[x] += sum;
  }
}
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &x);
    if (!x)
      Rt = i, f[i][0] = i;
    else
      f[i][0] = x, e[x].push_back(i);
  }
  Dfs(Rt);
  for (j = 1; j < 20; j++)
    for (i = 1; i <= n; i++) f[i][j] = f[f[i][j - 1]][j - 1];
  for (i = 1; i < n; i++) {
    if (!g[i].size()) break;
    for (j = 0; j < g[i].size(); j++) Ans[g[i][j]] += Ans[f[g[i][j]][0]] + i;
    Solve();
    reverse(g[i].begin(), g[i].end());
    Solve();
  }
  for (i = 1; i <= n; i++) printf("%I64d ", Ans[i]);
  return 0;
}
