#include <bits/stdc++.h>
using namespace std;
const int N = 500005;
const int P = 1e9 + 7;
const int inf = 1e9;
const long long Inf = 1e15;
inline int IN() {
  char ch = getchar();
  int x = 0, f = 0;
  while (ch < '0' || ch > '9') ch = getchar(), f = (ch == '-');
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + ch - 48;
    ch = getchar();
  }
  return f ? (-x) : x;
}
inline int Pow(int x, int y, int p) {
  int an = 1;
  for (; y; y >>= 1, x = (long long)x * x % p)
    if (y & 1) an = (long long)an * x % p;
  return an;
}
void renew(int &x, int y) {
  x += y;
  if (x < 0)
    x += P;
  else if (x >= P)
    x -= P;
}
void setIO(string a) {
  freopen((a + ".in").c_str(), "r", stdin);
  freopen((a + ".out").c_str(), "w", stdout);
}
template <typename T>
inline void chkmin(T &a, const T &b) {
  if (a > b) a = b;
}
template <typename T>
inline void chkmax(T &a, const T &b) {
  if (a < b) a = b;
}
int sz[N];
int n, rt, tot, seq[N << 1];
int dep[N], id[N], dfn[N], tim;
int gfa[N], stk[N], ed[N];
int Fa[20][N], *fa = Fa[0];
long long dp[N];
long long f[N];
vector<int> e[N];
void dfs(int x) {
  dfn[x] = ++tim;
  dep[x] = dep[fa[x]] + 1;
  for (int v : e[x]) dfs(v);
  ed[x] = tim;
}
int cmp(const int &a, const int &b) { return dep[a] < dep[b]; }
int cmpdfn(const int &a, const int &b) { return dfn[a] < dfn[b]; }
int lca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  for (int i = (int)19; i >= (int)0; i--)
    if (dep[Fa[i][x]] >= dep[y]) x = Fa[i][x];
  for (int i = (int)19; i >= (int)0; i--)
    if (Fa[i][x] ^ Fa[i][y]) x = Fa[i][x], y = Fa[i][y];
  return (x == y) ? x : fa[x];
}
int main() {
  scanf("%d", &n);
  for (int i = (int)1; i <= (int)n; i++) {
    fa[i] = IN();
    if (fa[i]) {
      int u = fa[i], v = i;
      e[u].push_back(v);
    }
  }
  for (int i = (int)1; i <= (int)n; i++)
    if (!fa[i]) rt = i;
  for (int i = (int)1; i <= (int)19; i++)
    for (int j = (int)1; j <= (int)n; j++) Fa[i][j] = Fa[i - 1][Fa[i - 1][j]];
  dfs(rt);
  for (int i = (int)1; i <= (int)n; i++) id[i] = i;
  sort(id + 1, id + n + 1, cmp);
  f[rt] = 1;
  for (int i = (int)2; i <= (int)n; i++) {
    int j = i;
    while (j <= n && dep[id[j]] == dep[id[i]]) ++j;
    --j;
    for (int k = (int)i; k <= (int)j; k++) {
      int u = id[k];
      f[u] = f[fa[u]];
    }
    int nowdep = dep[id[i]];
    tot = 0;
    for (int k = (int)i; k <= (int)j; k++) seq[++tot] = id[k];
    sort(seq + 1, seq + tot + 1, cmpdfn);
    int gtot = tot;
    for (int i = (int)1; i < (int)gtot; i++)
      seq[++tot] = lca(seq[i], seq[i + 1]);
    sort(seq + 1, seq + tot + 1, cmpdfn);
    tot = unique(seq + 1, seq + tot + 1) - (seq + 1);
    for (int i = (int)1; i <= (int)tot; i++) gfa[seq[i]] = 0;
    int top = 0;
    for (int i = (int)1; i <= (int)tot; i++) {
      int u = seq[i];
      while (top && ed[stk[top]] < dfn[u]) top--;
      gfa[u] = stk[top];
      stk[++top] = u;
    }
    for (int i = (int)1; i <= (int)tot; i++)
      if (dep[seq[i]] == nowdep)
        sz[seq[i]] = 1;
      else
        sz[seq[i]] = 0;
    for (int i = (int)tot; i >= (int)1; i--) {
      int u = seq[i];
      if (gfa[u]) sz[gfa[u]] += sz[u];
    }
    for (int i = (int)1; i <= (int)tot; i++) {
      int u = seq[i];
      if (gfa[u]) {
        dp[u] = dp[gfa[u]] + 1LL * dep[gfa[u]] * (sz[gfa[u]] - sz[u]);
      } else
        dp[u] = 0;
    }
    for (int i = (int)1; i <= (int)tot; i++)
      if (dep[seq[i]] == nowdep) f[seq[i]] += dp[seq[i]] + dep[seq[i]];
    i = j;
  }
  for (int i = (int)1; i <= (int)n; i++) printf("%lld ", f[i] - dep[i]);
  puts("");
  return 0;
}
