#include <bits/stdc++.h>
using namespace std;
int deep[500005], f[500005][19], fa[500005], cnt, dfn[500005];
pair<int, int> st[500005];
long long h[500005], g[500005];
vector<int> v[500005], to[500005];
void dfs(int x) {
  v[deep[x]].push_back(x);
  dfn[x] = ++cnt;
  for (auto tar : to[x]) {
    deep[tar] = deep[x] + 1;
    f[tar][0] = x;
    dfs(tar);
  }
}
bool cmp(int x, int y) { return dfn[x] < dfn[y]; }
int lca(int x, int y) {
  if (deep[x] < deep[y]) swap(x, y);
  for (int k = 18; k >= 0; k--)
    if (deep[f[x][k]] >= deep[y]) x = f[x][k];
  if (x == y) return x;
  for (int k = 18; k >= 0; k--)
    if (f[x][k] != f[y][k]) x = f[x][k], y = f[y][k];
  return f[x][0];
}
int main() {
  int n, rot;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &fa[i]);
    if (!fa[i])
      rot = i;
    else
      to[fa[i]].push_back(i);
  }
  deep[rot] = 1;
  dfs(rot);
  for (int k = 1; k <= 18; k++)
    for (int i = 1; i <= n; i++) f[i][k] = f[f[i][k - 1]][k - 1];
  for (int i = 1; i <= n; i++) {
    sort(v[i].begin(), v[i].end(), cmp);
    int top = 0;
    long long now = 0;
    st[0].first = -1;
    for (int j = 1; j < v[i].size(); j++) {
      int t = lca(v[i][j - 1], v[i][j]);
      while (top && deep[t] < deep[st[top].second])
        now -= deep[st[top].second] * (st[top].first - st[top - 1].first),
            top--;
      now += deep[t] * (j - 1 - st[top].first);
      h[v[i][j]] += now;
      st[++top] = make_pair(j - 1, t);
    }
    reverse(v[i].begin(), v[i].end());
    top = 0;
    now = 0;
    for (int j = 1; j < v[i].size(); j++) {
      int t = lca(v[i][j - 1], v[i][j]);
      while (top && deep[t] < deep[st[top].second])
        now -= deep[st[top].second] * (st[top].first - st[top - 1].first),
            top--;
      now += deep[t] * (j - 1 - st[top].first);
      h[v[i][j]] += now;
      st[++top] = make_pair(j - 1, t);
    }
    for (auto j : v[i]) g[j] = h[j] + g[fa[j]] + deep[fa[j]];
  }
  for (int i = 1; i <= n; i++) printf("%lld ", g[i]);
  return 0;
}
