#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const long long BIG = 1446803456761533460;
const int Big = 336860180;
stringstream sss;
const int maxn = 500010;
const int maxn2 = maxn * 2;
const int maxnlg = 20;
int n;
vector<int> subt[maxn];
int ht[maxn], st[maxn], ft[maxn], pars[maxnlg][maxn];
vector<int> levels[maxn];
int lev[maxn2], lt[maxn2], rt[maxn2];
long long ps[maxn2], val[maxn];
int tim = 0;
void dfs_init(int x) {
  st[x] = tim++;
  for (int i = (0); i < (maxnlg - 1); ++i) {
    pars[i + 1][x] = pars[i][pars[i][x]];
  }
  for (auto y : subt[x]) {
    ht[y] = ht[x] + 1;
    levels[ht[y]].emplace_back(y);
    pars[0][y] = x;
    dfs_init(y);
  }
  ft[x] = tim;
}
int LCA(int a, int b) {
  if (st[a] > st[b]) swap(a, b);
  if (ft[a] >= ft[b]) return a;
  for (int i = (maxnlg)-1; i >= (0); --i) {
    if (ft[pars[i][a]] < ft[b]) a = pars[i][a];
  }
  return pars[0][a];
}
void dfs_sum(int x) {
  for (auto y : subt[x]) {
    val[y] += val[x];
    dfs_sum(y);
  }
}
void MAIN() {
  cin >> n;
  int rot;
  for (int i = (0); i < (n); ++i) {
    int p;
    cin >> p;
    if (p == 0)
      rot = i;
    else
      subt[p - 1].emplace_back(i);
  }
  pars[0][rot] = rot;
  dfs_init(rot);
  for (int h = (1); h < (n); ++h) {
    if (levels[h].empty()) break;
    int m = 0;
    lev[m++] = levels[h][0];
    for (int i = (1); i < (levels[h].size()); ++i) {
      lev[m] = LCA(lev[m - 1], levels[h][i]);
      ++m;
      lev[m++] = levels[h][i];
    }
    lev[m++] = n;
    lt[0] = -1;
    for (int i = (1); i < (m - 1); ++i) {
      lt[i] = i - 1;
      while (lt[i] >= 0 && ht[lev[lt[i]]] >= ht[lev[i]]) lt[i] = lt[lt[i]];
    }
    rt[m - 2] = m - 1;
    for (int i = (m - 2) - 1; i >= (0); --i) {
      rt[i] = i + 1;
      while (rt[i] < m - 1 && ht[lev[rt[i]]] > ht[lev[i]]) rt[i] = rt[rt[i]];
    }
    fill(ps, ps + m, 0);
    for (int i = 1; i < m - 1; i += 2) {
      long long l = 1LL * (ht[lev[i]] + 1) * (rt[i] - i) / 2;
      long long r = 1LL * (ht[lev[i]] + 1) * (i - lt[i]) / 2;
      ps[lt[i] + 1] += l, ps[i] -= l;
      ps[i + 1] += r, ps[rt[i]] -= r;
    }
    for (int i = (0); i < (m - 1); ++i) ps[i + 1] += ps[i];
    for (int i = 0; i < m - 1; i += 2) {
      val[lev[i]] += ps[i] + ht[lev[i]];
    }
  }
  dfs_sum(rot);
  for (int i = (0); i < (n); ++i) cout << val[i] << ' ';
  cout << '\n';
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cout << fixed << setprecision(10);
  sss << R"(
5
2 3 4 5 0
    )";
  MAIN();
  return 0;
}
