#include <bits/stdc++.h>
using namespace std;
const int N = 500005;
const int K = 20;
vector<int> adj[N], vert[N];
int ptr, anc[N], cnt[N], st[N], en[N];
int parent[N], depth[N];
long long answer[N];
void dfs(int v, int d) {
  st[v] = ptr++;
  depth[v] = d;
  vert[d].push_back(v);
  for (int u : adj[v]) {
    dfs(u, d + 1);
  }
  en[v] = ptr - 1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> parent[i];
    adj[parent[i]].push_back(i);
    answer[i] = 0;
  }
  assert(adj[0].size() == 1);
  int root = adj[0][0];
  ptr = 1;
  dfs(root, 1);
  for (int d = 1; d <= n; d++) {
    if (vert[d].empty()) break;
    for (auto u : vert[d]) {
      answer[u] = answer[parent[u]] + depth[u] - 1;
      if (u != root && adj[parent[u]].size() == 1) {
        parent[u] = parent[parent[u]];
      }
    }
    int siz = 0;
    long long sum = 0;
    for (auto u : vert[d]) {
      int i;
      for (i = siz - 1; i >= 0; i--) {
        int w = anc[i];
        while (st[u] < st[w] || en[w] < st[u]) {
          w = parent[w];
        }
        if (w == anc[i]) break;
        sum -= cnt[i] * 1LL * depth[anc[i]];
        anc[i] = w;
        sum += cnt[i] * 1LL * depth[anc[i]];
      }
      for (int j = i + 1; j < siz; j++) {
        if (i == -1) {
          anc[i + 1] = anc[j];
          cnt[i + 1] = cnt[j];
          i++;
        } else if (anc[i] == anc[j]) {
          cnt[i] += cnt[j];
        } else {
          anc[i + 1] = anc[j];
          cnt[i + 1] = cnt[j];
          i++;
        }
      }
      siz = i + 1;
      answer[u] += sum;
      anc[siz] = u;
      cnt[siz] = 1;
      siz++;
      sum += depth[u];
    }
    siz = 0;
    sum = 0;
    reverse(vert[d].begin(), vert[d].end());
    for (auto u : vert[d]) {
      int i;
      for (i = siz - 1; i >= 0; i--) {
        int w = anc[i];
        while (st[u] < st[w] || en[w] < st[u]) {
          w = parent[w];
        }
        if (w == anc[i]) break;
        sum -= cnt[i] * 1LL * depth[anc[i]];
        anc[i] = w;
        sum += cnt[i] * 1LL * depth[anc[i]];
      }
      for (int j = i + 1; j < siz; j++) {
        if (i == -1) {
          anc[i + 1] = anc[j];
          cnt[i + 1] = cnt[j];
          i++;
        } else if (anc[i] == anc[j]) {
          cnt[i] += cnt[j];
        } else {
          anc[i + 1] = anc[j];
          cnt[i + 1] = cnt[j];
          i++;
        }
      }
      siz = i + 1;
      answer[u] += sum;
      anc[siz] = u;
      cnt[siz] = 1;
      siz++;
      sum += depth[u];
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << answer[i] << " ";
  }
  cout << '\n';
  return 0;
}
