#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int SIZE = 1e6;
vector<int> e[SIZE];
vector<int> gg[SIZE];
int jump[20][SIZE], lv[SIZE];
void __init(int x, int lt) {
  gg[lv[x]].push_back(x);
  jump[0][x] = lt;
  for (int i = 0; i < (((int)(e[x]).size())); ++i) {
    int y = e[x][i];
    if (y != lt) {
      lv[y] = lv[x] + 1;
      __init(y, x);
    }
  }
}
int adv(int x, int v) {
  for (int i = 0; (1 << i) <= v; i++) {
    if ((v >> i) & 1) x = jump[i][x];
  }
  return x;
}
int lca(int x, int y) {
  if (lv[x] > lv[y])
    x = adv(x, lv[x] - lv[y]);
  else
    y = adv(y, lv[y] - lv[x]);
  if (x == y) return x;
  for (int i = 19; i >= 0; i--)
    if (jump[i][x] != jump[i][y]) {
      x = jump[i][x];
      y = jump[i][y];
    }
  return jump[0][x];
}
void build(int root, int N) {
  __init(root, root);
  for (int i = (1); i < (20); ++i) {
    for (int x = (1); x < (N + 1); ++x)
      jump[i][x] = jump[i - 1][jump[i - 1][x]];
  }
}
long long an[SIZE];
int h[SIZE];
pair<int, int> stk[SIZE];
void handle(int now_lv) {
  int m = ((int)(gg[now_lv]).size());
  for (int i = 0; i < (m); ++i) {
    int x = gg[now_lv][i];
    an[x] = an[jump[0][x]] + lv[x];
  }
  for (int i = (1); i < (m); ++i) {
    h[i] = lv[lca(gg[now_lv][i - 1], gg[now_lv][i])] + 1;
  }
  long long now = 0;
  int sn = 0;
  for (int i = (1); i < (m); ++i) {
    int num = 1;
    while (sn && h[i] <= stk[sn - 1].first) {
      now -= (long long)stk[sn - 1].first * stk[sn - 1].second;
      num += stk[--sn].second;
    }
    now += (long long)h[i] * num;
    stk[sn++] = make_pair(h[i], num);
    an[gg[now_lv][i]] += now;
  }
  sn = 0;
  now = 0;
  for (int i = m - 1; i > 0; i--) {
    int num = 1;
    while (sn && h[i] <= stk[sn - 1].first) {
      now -= (long long)stk[sn - 1].first * stk[sn - 1].second;
      num += stk[--sn].second;
    }
    now += (long long)h[i] * num;
    stk[sn++] = make_pair(h[i], num);
    an[gg[now_lv][i - 1]] += now;
  }
}
int main() {
  int root = 0;
  int(N);
  scanf("%d", &N);
  for (int i = (1); i < (N + 1); ++i) {
    int(x);
    scanf("%d", &x);
    if (!x)
      root = i;
    else
      e[x].push_back(i);
  }
  build(root, N);
  for (int i = (1); i < (SIZE); ++i) {
    if (!((int)(gg[i]).size())) break;
    handle(i);
  }
  for (int i = (1); i < (N + 1); ++i) printf("%I64d ", an[i]);
  return 0;
}
