#include <bits/stdc++.h>
using namespace std;
const int Imx = 2147483647;
const long long Lbig = 2e18;
const int mod = 1e9 + 7;
struct fastio {
  char s[100000];
  int it, len;
  fastio() { it = len = 0; }
  inline char get() {
    if (it < len) return s[it++];
    it = 0;
    len = fread(s, 1, 100000, stdin);
    if (len == 0)
      return EOF;
    else
      return s[it++];
  }
  bool notend() {
    char c = get();
    while (c == ' ' || c == '\n') c = get();
    if (it > 0) it--;
    return c != EOF;
  }
} _buff;
inline long long getnum() {
  long long r = 0;
  bool ng = 0;
  char c;
  c = _buff.get();
  while (c != '-' && (c < '0' || c > '9')) c = _buff.get();
  if (c == '-') ng = 1, c = _buff.get();
  while (c >= '0' && c <= '9') r = r * 10 + c - '0', c = _buff.get();
  return ng ? -r : r;
}
template <class T>
inline void putnum(T x) {
  if (x < 0) putchar('-'), x = -x;
  register short a[20] = {}, sz = 0;
  while (x) a[sz++] = x % 10, x /= 10;
  if (sz == 0) putchar('0');
  for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
inline char getreal() {
  char c = _buff.get();
  while (c == ' ' || c == '\n') c = _buff.get();
  return c;
}
int n, p[500111], lv[500111];
vector<int> con[500111];
int head[500111], to[500111], nxt[500111], etot;
int q[500111], qn, qid[500111];
void adde(int x, int y) {
  nxt[++etot] = head[x];
  head[x] = etot;
  to[etot] = y;
}
int mxl[500111], mxu[500111], st[500111], csz[500111];
pair<pair<int, int>, int> tab[500111];
long long ans[500111];
void merge(int x, int y, long long coef) {
  ans[tab[x].first.first - 1] -= tab[y].second * coef;
  ans[tab[x].first.second] += tab[y].second * coef;
  ans[tab[y].first.first - 1] -= tab[x].second * coef;
  ans[tab[y].first.second] += tab[x].second * coef;
  tab[x].first.first = min(tab[x].first.first, tab[y].first.first);
  tab[x].first.second = max(tab[x].first.second, tab[y].first.second);
  tab[x].second += tab[y].second;
}
int tmp[500111], tmpsz;
int main() {
  n = getnum();
  int rt = 0;
  for (int i = 1; i <= n; i++) {
    p[i] = getnum();
    if (p[i] == 0) rt = i;
    con[p[i]].push_back(i);
  }
  q[++qn] = rt;
  lv[1] = 1;
  p[1] = 0;
  for (int i = 1; i <= qn; i++) {
    int u = q[i];
    qid[u] = i;
    for (int j = 0; j < con[u].size(); j++) {
      int v = con[u][j];
      q[++qn] = v;
      p[qn] = i;
      adde(i, qn);
      lv[qn] = lv[i] + 1;
    }
  }
  for (int x = n; x >= 1; x--) {
    mxl[x] = x;
    for (int i = head[x]; i; i = nxt[i]) {
      int u = to[i];
      if (lv[mxl[u]] > lv[mxl[x]]) mxl[x] = mxl[u], mxu[x] = u;
    }
    csz[x] = lv[mxl[x]] - lv[x];
    st[mxl[x] + 1]++;
  }
  for (int i = 1; i <= n; i++) st[i] += st[i - 1];
  for (int x = n; x >= 1; x--) {
    int g = mxl[x];
    tmpsz = 0;
    for (int i = head[x]; i; i = nxt[i]) {
      tmp[tmpsz++] = to[i];
      if (mxu[x] == to[i]) reverse(tmp, tmp + tmpsz);
    }
    for (int i = 1; i < tmpsz; i++) {
      int u = tmp[i];
      for (int j = 0; j <= csz[u]; j++) {
        int k = csz[x] - 1 - (csz[u] - j);
        merge(st[g] + k, st[mxl[u]] + j, lv[x]);
      }
    }
    tab[st[g] + csz[x]] = make_pair(make_pair(x, x), 1);
  }
  for (int i = n; i >= 1; i--) ans[i] += ans[i + 1];
  for (int i = 2; i <= n; i++) ans[i] += ans[p[i]] + lv[i] - 1;
  for (int i = 1; i <= n; i++) putnum(ans[qid[i]]), putchar(' ');
  return 0;
}
