#include <bits/stdc++.h>
int inp() {
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  int sum = 0;
  while (c >= '0' && c <= '9') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return sum;
}
int head[500010], nxt[1000010], end[1000010], dfn[500010], depth[500010],
    fa[500010][23];
std::vector<int> vec[500010];
int idx, cou, n;
long long ans[500010];
void link(int a, int b) {
  nxt[++cou] = head[a];
  head[a] = cou;
  end[cou] = b;
}
void dfs(int cur, int last) {
  dfn[cur] = ++idx;
  fa[cur][0] = last;
  depth[cur] = depth[last] + 1;
  for (int i = 1; i <= 20; i++) fa[cur][i] = fa[fa[cur][i - 1]][i - 1];
  for (int x = head[cur]; x != -1; x = nxt[x])
    if (end[x] != last) dfs(end[x], cur);
}
int lca(int a, int b) {
  if (depth[a] < depth[b]) std::swap(a, b);
  int delta = depth[a] - depth[b];
  for (int i = 0; i <= 20; i++)
    if (delta & (1 << i)) a = fa[a][i];
  if (a == b) return a;
  for (int i = 20; i >= 0; i--) {
    if (fa[a][i] != fa[b][i]) {
      a = fa[a][i];
      b = fa[b][i];
    }
  }
  return fa[a][0];
}
bool cmp(int a, int b) { return dfn[a] < dfn[b]; }
struct Node {
  int x;
  int depth;
  int id;
} s[500010];
long long calc(int top) {
  return (long long)(s[top].depth) * (s[top].id - s[top - 1].id);
}
void dfs2(int cur, int last) {
  for (int x = head[cur]; x != -1; x = nxt[x]) {
    if (end[x] != last) {
      ans[end[x]] += ans[cur] + depth[cur];
      dfs2(end[x], cur);
    }
  }
}
void solve() {
  for (int d = 1; d <= n; d++) {
    if (!vec[d].size()) break;
    int top = 1;
    s[top] = (Node){vec[d][0], 0, 0};
    long long sum = 0;
    for (int i = 1; i < vec[d].size(); i++) {
      while (depth[lca(vec[d][i], s[top].x)] <= s[top].depth) {
        sum -= calc(top);
        top--;
      }
      int _lca = lca(vec[d][i], s[top].x);
      s[++top] = (Node){vec[d][i], depth[_lca], i};
      sum += calc(top);
      ans[vec[d][i]] += sum;
    }
  }
}
int main() {
  memset(head, -1, sizeof(head));
  n = inp();
  int rt;
  for (int i = 1; i <= n; i++) {
    int f = inp();
    if (f)
      link(f, i);
    else
      rt = i;
  }
  dfs(rt, 0);
  for (int i = 1; i <= n; i++) vec[depth[i]].push_back(i);
  for (int i = 1; i <= n; i++)
    if (vec[i].size()) std::sort(vec[i].begin(), vec[i].end(), cmp);
  solve();
  for (int i = 1; i <= n; i++)
    if (vec[i].size()) std::reverse(vec[i].begin(), vec[i].end());
  solve();
  dfs2(rt, 0);
  for (int i = 1; i <= n; i++) printf("%lld ", ans[i]);
  putchar('\n');
}
