#include <bits/stdc++.h>
using namespace std;
const long long maxn = 5e5 + 10, inf = 1e9 + 10, lg = 21;
long long n, root, H[maxn], par[maxn][lg];
vector<long long> G[maxn], v[maxn];
long long st[maxn], ft[maxn], se, strev[maxn];
bool mark[maxn];
void dfs(long long a, long long p = root) {
  par[a][0] = p;
  for (long long i = 1; i < lg; i++) {
    par[a][i] = par[par[a][i - 1]][i - 1];
  }
  v[H[a]].push_back(a);
  strev[se] = a;
  st[a] = se++;
  for (long long b : G[a]) {
    H[b] = H[a] + 1;
    dfs(b, a);
  }
  ft[a] = se;
}
long long lca(long long a, long long b) {
  if (H[a] > H[b]) swap(a, b);
  for (long long i = lg - 1; i >= 0; i--) {
    if (H[par[b][i]] >= H[a]) {
      b = par[b][i];
    }
  }
  if (a == b) return a;
  for (long long i = lg - 1; i >= 0; i--) {
    if (par[a][i] != par[b][i]) {
      a = par[a][i];
      b = par[b][i];
    }
  }
  return par[a][0];
}
long long ans[maxn], sum[maxn];
long long lc[maxn], ts[maxn];
int32_t main() {
  scanf("%lld", &n);
  for (long long i = 0; i < n; i++) {
    long long a;
    scanf("%lld", &a);
    if (a == 0) {
      root = i;
    } else {
      G[a - 1].push_back(i);
    }
  }
  dfs(root);
  for (long long h = 1; h < n; h++) {
    long long size = v[h].size();
    for (long long i = 0; i < size; i++) {
      ts[i] = st[v[h][i]];
    }
    for (long long i = 0; i < size - 1; i++) {
      lc[i] = lca(v[h][i], v[h][i + 1]);
    }
    for (long long i = 0; i < size; i++) {
      long long plus = H[v[h][i]] + 1;
      if (i > 0) {
        plus = min(plus, H[v[h][i]] - H[lc[i - 1]]);
      }
      if (i < size - 1) {
        plus = min(plus, H[v[h][i]] - H[lc[i]]);
      }
      ans[v[h][i]] = plus - 1;
    }
    for (long long i = 0; i < size - 1; i++) {
      if (mark[lc[i]]) continue;
      mark[lc[i]] = 1;
      long long l = lower_bound(ts, ts + size, st[lc[i]]) - ts;
      long long r = lower_bound(ts, ts + size, ft[lc[i]]) - ts;
      long long x = H[lc[i]] + 1;
      if (r != size) {
        x = min(x, H[lc[i]] - H[lca(lc[i], v[h][r])]);
      }
      if (l - 1 != -1) {
        x = min(x, H[lc[i]] - H[lca(lc[i], v[h][l - 1])]);
      }
      sum[l] += x * (r - l);
      sum[r] -= x * (r - l);
    }
    for (long long i = 1; i < size; i++) {
      sum[i] += sum[i - 1];
    }
    for (long long i = 0; i < size; i++) {
      ans[v[h][i]] += sum[i] + ans[par[v[h][i]][0]];
    }
    for (long long i = 0; i < size - 1; i++) {
      mark[lc[i]] = 0;
    }
    for (long long i = 0; i < size + 1; i++) {
      sum[i] = 0;
    }
  }
  for (long long i = 0; i < n; i++) {
    printf("%lld ", ans[i]);
  }
  printf("\n");
}
