#include <bits/stdc++.h>
using namespace std;
const int MX = 5e5 + 10, MD = 1e9 + 7, LG = 20;
int n, m, a[MX], ans2[MX], tmp, p[MX], par[MX][LG + 10], h[MX], st[MX], en[MX],
    Time, root;
long long res[MX], ans;
vector<int> adj[MX], stage[MX];
vector<pair<int, int>> lca;
void pre_dfs(int v) {
  st[v] = Time++;
  stage[h[v]].push_back(v);
  for (int i = 1; i < LG; i++) par[v][i] = par[par[v][i - 1]][i - 1];
  for (auto u : adj[v]) {
    if (u == p[v]) continue;
    par[u][0] = v;
    h[u] = h[v] + 1;
    pre_dfs(u);
  }
  en[v] = Time;
}
bool ancestor(int u, int v) { return st[u] <= st[v] && en[u] >= en[v]; }
int getpar(int v, int k) {
  for (int i = LG - 1; i >= 0; i--)
    if (k & (1 << i)) v = par[v][i];
  return v;
}
int LCA(int u, int v) {
  for (int i = LG - 1; i >= 0; i--) {
    if (par[v][i] != par[u][i]) u = par[u][i], v = par[v][i];
  }
  return par[v][0];
}
void dfs(int v) {
  for (auto u : adj[v]) {
    res[u] = h[u] + ans2[u] + res[v];
    dfs(u);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &p[i]);
    if (p[i])
      adj[p[i]].push_back(i);
    else
      root = i;
  }
  pre_dfs(root);
  for (int i = 0; i < MX; i++) {
    if (!stage[i].size()) break;
    for (int rev = 0; rev < 2; rev++) {
      stack<int> st;
      lca.clear();
      ans = 0;
      for (int j = 0; j < stage[i].size() - 1; j++)
        lca.push_back({h[LCA(stage[i][j], stage[i][j + 1])] + 1, 1});
      for (int j = 0; j < stage[i].size() - 1; j++) {
        while (!st.empty() && lca[st.top()].first > lca[j].first) {
          lca[j].second += lca[st.top()].second;
          ans -= 1ll * lca[st.top()].first * lca[st.top()].second;
          st.pop();
        }
        ans += 1ll * lca[j].first * lca[j].second;
        ans2[stage[i][j + 1]] += ans;
        st.push(j);
      }
      reverse(stage[i].begin(), stage[i].end());
    }
  }
  dfs(root);
  for (int i = 1; i <= n; i++) printf("%lld ", res[i]);
}
