#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int MOD2 = 1007681537;
const int INF = (int)1e9;
const long long LINF = (long long)1e18;
const long double PI = acos((long double)-1);
const long double EPS = 1e-9;
inline long long gcd(long long a, long long b) {
  long long r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long fpow(long long n, long long k, int p = MOD) {
  long long r = 1;
  for (; k; k >>= 1) {
    if (k & 1) r = r * n % p;
    n = n * n % p;
  }
  return r;
}
template <class T>
inline int chkmin(T& a, const T& val) {
  return val < a ? a = val, 1 : 0;
}
template <class T>
inline int chkmax(T& a, const T& val) {
  return a < val ? a = val, 1 : 0;
}
inline long long isqrt(long long k) {
  long long r = sqrt(k) + 1;
  while (r * r > k) r--;
  return r;
}
inline long long icbrt(long long k) {
  long long r = cbrt(k) + 1;
  while (r * r * r > k) r--;
  return r;
}
inline void addmod(int& a, int val, int p = MOD) {
  if ((a = (a + val)) >= p) a -= p;
}
inline void submod(int& a, int val, int p = MOD) {
  if ((a = (a - val)) < 0) a += p;
}
inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }
inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; }
inline int sign(long double x, long double y) { return sign(x - y); }
template <class T>
struct RangeFenwick {
  static const int maxn = 5e5 + 5;
  int n;
  T fen0[maxn];
  long long fen1[maxn];
  RangeFenwick(int n) { this->n = n; }
  inline void upd(int p, T val) {
    p++;
    assert(p > 0);
    for (int pp = p; pp <= n; pp += pp & -pp) {
      fen0[pp] += val;
      fen1[pp] += val * p;
    }
  }
  void upd(int l, int r, T val) { upd(l, +val), upd(r + 1, -val); }
  inline long long query(int p) {
    p++;
    assert(p <= n);
    long long res = 0;
    for (int pp = p; pp > 0; pp -= pp & -pp) {
      res += (long long)fen0[pp] * (p + 1);
      res -= fen1[pp];
    }
    return res;
  }
};
const int maxn = 5e5 + 5;
int n;
vector<int> adj[maxn];
int size[maxn];
int lev[maxn];
int p[maxn];
int heavy[maxn];
int num[maxn];
int head[maxn];
int cnt;
RangeFenwick<int> fen(maxn);
long long ans[maxn];
inline void upd(int l, int r, int n, int val) { fen.upd(l, r, val); }
inline long long query(int l, int r, int n) {
  return fen.query(r) - fen.query(l - 1);
}
void firstdfs(int u, int dad) {
  size[u] = 1, heavy[u] = -1;
  for (int i = 0; i < adj[u].size(); i++) {
    int v = adj[u][i];
    if (v != dad) {
      p[v] = u, lev[v] = lev[u] + 1;
      firstdfs(v, u);
      size[u] += size[v];
      if (heavy[u] == -1 || size[heavy[u]] < size[v]) {
        heavy[u] = v;
      }
    }
  }
}
void dfs(int u, int h, int p, long long sumsize = 0) {
  num[u] = cnt++, head[u] = h;
  sumsize += size[u];
  ans[u] = sumsize;
  if (~heavy[u]) {
    dfs(heavy[u], h, u, sumsize);
  }
  for (int i = 0; i < adj[u].size(); i++) {
    int v = adj[u][i];
    if (v != p && v != heavy[u]) {
      dfs(v, v, u, sumsize);
    }
  }
}
void upd(int u, int v, int val) {
  int hu = head[u], hv = head[v];
  while (hu != hv) {
    if (lev[hu] < lev[hv]) swap(u, v), swap(hu, hv);
    upd(num[hu], num[u], n, val);
    u = p[hu], hu = head[u];
  }
  if (lev[u] > lev[v]) swap(u, v);
  upd(num[u], num[v], n, val);
}
long long query(int u, int v) {
  long long res = 0;
  int hu = head[u], hv = head[v];
  while (hu != hv) {
    if (lev[hu] < lev[hv]) swap(u, v), swap(hu, hv);
    res += query(num[hu], num[u], n);
    u = p[hu], hu = head[u];
  }
  if (lev[u] > lev[v]) swap(u, v);
  res += query(num[u], num[v], n);
  return res;
}
void solve() {
  scanf("%d", &n);
  int rt = 0;
  for (int i = (0); i < (n); i++) {
    int x;
    scanf("%d", &x);
    if (x) {
      x--;
      adj[x].push_back(i);
    } else {
      rt = i;
    }
  }
  firstdfs(rt, -1), dfs(rt, rt, -1);
  vector<pair<int, int> > v;
  for (int i = (0); i < (n); i++) v.push_back(make_pair(lev[i], i));
  sort((v).begin(), (v).end(), greater<pair<int, int> >());
  int ptr = 0;
  for (int i = (0); i < (n); i++) {
    while (ptr < n && v[ptr].first > v[i].first) {
      int u = v[ptr].second;
      upd(rt, u, -1);
      ptr++;
    }
    int u = v[i].second;
    ans[u] += query(rt, u) - lev[u] - 1;
  }
  for (int i = (0); i < (n); i++) printf("%I64d%c", ans[i], " \n"[i == n - 1]);
}
int main() {
  int JUDGE_ONLINE = 1;
  if (fopen("in.txt", "r")) {
    JUDGE_ONLINE = 0;
    assert(freopen("in.txt", "r", stdin));
  } else {
  }
  solve();
  if (!JUDGE_ONLINE) {
  }
  return 0;
}
