#include <bits/stdc++.h>
using namespace std;
const int MN = 500010;
int N;
vector<int> Vd[MN], adj[MN];
int tin[MN], tout[MN], invt[MN], dep[MN], par[20][MN], timer;
void dfs0(int u) {
  tin[u] = timer++;
  Vd[dep[u]].push_back(tin[u]);
  for (int i = 1; i < 20; i++) {
    int t = par[i - 1][u];
    if (t == -1) break;
    par[i][u] = par[i - 1][t];
  }
  for (int i = 0; i < adj[u].size(); i++) {
    int v = adj[u][i];
    dep[v] = dep[u] + 1;
    par[0][v] = u;
    dfs0(v);
  }
  tout[u] = timer;
}
int lca(int a, int b) {
  if (dep[a] < dep[b]) swap(a, b);
  int diff = dep[a] - dep[b];
  for (int i = 0; i < 20; i++) {
    if (diff & (1 << i)) a = par[i][a];
  }
  if (a == b) return a;
  for (int i = 20; i--;) {
    if (par[i][a] != par[i][b]) {
      a = par[i][a];
      b = par[i][b];
    }
  }
  return par[0][a];
}
vector<int> tadj[MN];
stack<int> stk;
long long dp[MN];
int ttin[MN], ttout[MN], ttimer, sz[MN];
void dfs1(int u) {
  ttin[u] = ttimer++;
  sz[u] = tadj[u].size() == 0;
  for (int i = 0; i < tadj[u].size(); i++) {
    int v = tadj[u][i];
    dfs1(v);
    sz[u] += sz[v];
  }
  ttout[u] = ttimer;
}
void dfs2(int u, long long sum) {
  if (tadj[u].size() == 0) dp[u] += sum;
  for (int i = 0; i < tadj[u].size(); i++) {
    int v = tadj[u][i];
    dfs2(v, sum + 1LL * (dep[u] + 1) * (sz[u] - sz[v]));
  }
}
int main() {
  scanf("%d", &N);
  int root;
  for (int i = 0; i < N; i++) {
    int p;
    scanf("%d", &p);
    p--;
    if (p == -1)
      root = i;
    else
      adj[p].push_back(i);
  }
  memset(par, -1, sizeof(par));
  dfs0(root);
  for (int i = 0; i < N; i++) invt[tin[i]] = i;
  for (int d = 0; d < N; d++) {
    if (Vd[d].size() == 0) break;
    Vd[d].push_back(0);
    vector<int> V;
    while (!stk.empty()) stk.pop();
    stk.push(invt[Vd[d][0]]);
    V.push_back(invt[Vd[d][0]]);
    for (int i = 1; i < Vd[d].size(); i++) {
      int u = invt[Vd[d][i]];
      int p = invt[Vd[d][i - 1]];
      int x = lca(p, u);
      V.push_back(u);
      V.push_back(x);
      int la = -1;
      while (!stk.empty() && dep[stk.top()] > dep[x]) {
        int t = stk.top();
        stk.pop();
        if (la != -1) tadj[t].push_back(la);
        la = t;
      }
      if (la != -1) tadj[x].push_back(la);
      if (stk.empty() || x != stk.top()) stk.push(x);
      stk.push(u);
    }
    ttimer = 0;
    dfs1(root);
    dfs2(root, 0);
    for (int i = 0; i < (int)Vd[d].size() - 1; i++) {
      int u = invt[Vd[d][i]];
      for (int j = 0; j < adj[u].size(); j++) {
        int v = adj[u][j];
        dp[v] += dp[u] + dep[u] + 1;
      }
    }
    for (int i = 0; i < V.size(); i++) tadj[V[i]].clear();
  }
  for (int i = 0; i < N; i++) {
    printf("%lld ", dp[i]);
  }
}
