#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;
}
struct SGT {
  long long a[2000111];
  int lz[2000111];
  void init() {
    memset(a, 0, sizeof(a));
    memset(lz, 0, sizeof(lz));
  }
  SGT() { init(); }
  void pushdown(int p, int l, int r, int m) {
    if (lz[p] != 0) {
      lz[p << 1] += lz[p];
      lz[p << 1 | 1] += lz[p];
      a[p << 1] += 1ll * lz[p] * (m - l + 1);
      a[p << 1 | 1] += 1ll * lz[p] * (r - m);
      lz[p] = 0;
    }
  }
  void add(int x, int y, int l, int r, int p) {
    if (x <= l && r <= y) {
      lz[p]++;
      a[p] += (r - l + 1);
      return;
    }
    int m = l + r >> 1;
    pushdown(p, l, r, m);
    if (x <= m) add(x, y, l, m, p << 1);
    if (m < y) add(x, y, m + 1, r, p << 1 | 1);
    a[p] = a[p << 1] + a[p << 1 | 1];
  }
  long long query(int x, int y, int l, int r, int p) {
    if (x <= l && r <= y) return a[p];
    int m = l + r >> 1;
    pushdown(p, l, r, m);
    if (x <= m && m < y)
      return query(x, y, l, m, p << 1) + query(x, y, m + 1, r, p << 1 | 1);
    else if (x <= m)
      return query(x, y, l, m, p << 1);
    else
      return query(x, y, m + 1, r, p << 1 | 1);
  }
  void out() {
    for (int i = 1; i <= 4; i++) cerr << a[i] << "," << lz[i] << " ";
    cerr << endl;
  }
} sgt;
int n;
int p[500111], lv[500111], sz[500111];
int dfn[500111], top[500111], mxs[500111], dfntot;
int q[500111], qn;
int nq[500111], nqn;
vector<int> son[500111];
long long ans[500111];
void addroute(int x) {
  while (x) {
    sgt.add(dfn[top[x]], dfn[x], 1, n, 1);
    x = p[top[x]];
  }
}
long long queryroute(int x) {
  long long ans = 0;
  while (x) {
    ans += sgt.query(dfn[top[x]], dfn[x], 1, n, 1);
    x = p[top[x]];
  }
  return ans;
}
void dfs(int x) {
  sz[x] = 1;
  for (int i = 0; i < son[x].size(); i++) {
    int u = son[x][i];
    lv[u] = lv[x] + 1;
    dfs(u);
    sz[x] += sz[u];
  }
}
void dfs2(int x) {
  dfn[x] = ++dfntot;
  for (int i = 0; i < son[x].size(); i++) {
    int u = son[x][i];
    if (sz[u] > sz[mxs[x]]) mxs[x] = u;
  }
  if (mxs[x]) {
    top[mxs[x]] = top[x];
    dfs2(mxs[x]);
  }
  for (int i = 0; i < son[x].size(); i++) {
    int u = son[x][i];
    if (u == mxs[x]) continue;
    top[u] = u;
    dfs2(u);
  }
}
int main() {
  n = getnum();
  for (int i = 1; i <= n; i++) {
    p[i] = getnum();
    son[p[i]].push_back(i);
  }
  int rt = son[0][0];
  lv[rt] = 1;
  dfs(rt);
  top[rt] = rt;
  dfs2(rt);
  q[qn++] = rt;
  while (true) {
    if (qn == 0) break;
    for (int i = 0; i < qn; i++) {
      int u = q[i];
      for (int j = 0; j < son[u].size(); j++) {
        int v = son[u][j];
        nq[nqn++] = v;
        lv[v] = lv[u] + 1;
        addroute(v);
      }
    }
    for (int i = 0; i < nqn; i++) {
      ans[nq[i]] = queryroute(nq[i]) - lv[nq[i]] + 1;
    }
    for (int j = 0; j < nqn; j++) q[j] = nq[j];
    qn = nqn;
    nqn = 0;
  }
  for (int i = 1; i <= n; i++) {
    putnum(ans[i]), putchar(' ');
  }
  return 0;
}
