#include <bits/stdc++.h>
#pragma GCC optimize("O2,Ofast,inline,unroll-all-loops,-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,popcnt")
using namespace std;
template <class T>
inline void in(T &x) {
  x = 0;
  char c = getchar();
  bool f = 0;
  while (!isdigit(c)) f |= (c == '-'), c = getchar();
  while (isdigit(c)) x = x * 10 + (c ^ '0'), c = getchar();
  f ? x = -x : 0;
}
template <class T>
inline void out(T x, const char c = '\n') {
  static short st[30];
  short m = 0;
  if (x < 0) putchar('-'), x = -x;
  do st[++m] = x % 10, x /= 10;
  while (x);
  while (m) putchar(st[m--] | '0');
  putchar(c);
}
template <class T>
inline void err(const T &x, const char c = '\n') {
  cerr << x << c;
}
template <class T, class... Args>
inline void in(T &x, Args &...args) {
  in(x);
  in(args...);
}
template <class T, class... Args>
inline void out(const T &x, const Args &...args) {
  out(x, ' ');
  out(args...);
}
template <class T, class... Args>
inline void err(const T &x, const Args &...args) {
  err(x, ' ');
  err(args...);
}
template <class T>
inline void prt(T a[], int n) {
  for (register int i = 0; i < n; ++i) out(a[i], i == n - 1 ? '\n' : ' ');
}
template <class T>
inline void clr(T a[], int n) {
  memset(a, 0, sizeof(T) * n);
}
template <class T>
inline void clr(T *a, T *b) {
  memset(a, 0, sizeof(T) * (b - a));
}
template <class T>
inline bool ckmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T>
inline bool ckmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
namespace MOD_CALC {
const int md = 1e9 + 7, inv2 = (md + 1) / 2;
inline int add(const int a, const int b) {
  return a + b >= md ? a + b - md : a + b;
}
inline int sub(const int a, const int b) {
  return a - b < 0 ? a - b + md : a - b;
}
inline int mul(const int a, const int b) { return (long long)a * b % md; }
inline void inc(int &a, const int b) { (a += b) >= md ? a -= md : 0; }
inline void dec(int &a, const int b) { (a -= b) < 0 ? a += md : 0; }
inline int qpow(int a, int b) {
  int r = 1;
  for (; b; b >>= 1, a = mul(a, a))
    if (b & 1) r = mul(r, a);
  return r;
}
inline int qpow(int a, int b, const int p) {
  int r = 1;
  for (; b; b >>= 1, a = (long long)a * a % p)
    if (b & 1) r = (long long)r * a % p;
  return r;
}
inline int mdinv(const int a) { return qpow(a, md - 2); }
template <class... Args>
inline int add(const int a, const int b, const Args &...args) {
  return add(add(a, b), args...);
}
template <class... Args>
inline int mul(const int a, const int b, const Args &...args) {
  return mul(mul(a, b), args...);
}
}  // namespace MOD_CALC
using namespace MOD_CALC;
namespace i207M {
int n;
vector<int> sn[500005];
int mxd;
int dep[500005], fa[500005];
int eu[500005 * 2][20], dk, dfn[500005], lg[500005 * 2], ys[500005 * 2];
vector<int> hv[500005];
void dfs(int x, int _fa) {
  hv[dep[x] = dep[_fa] + 1].emplace_back(x);
  ckmax(mxd, dep[x]);
  ys[dfn[x] = ++dk] = x, eu[dk][0] = dk;
  for (const auto &v : sn[x]) {
    dfs(v, x);
    eu[++dk][0] = dfn[x];
  }
  sn[x].clear();
}
void prework() {
  for (register int i = 2; i <= dk; ++i) lg[i] = lg[i >> 1] + 1;
  for (register int j = 1; j <= 19; ++j)
    for (register int i = 1; i + (1 << j) - 1 <= dk; ++i)
      eu[i][j] = min(eu[i][j - 1], eu[i + (1 << (j - 1))][j - 1]);
}
inline int lca(int l, int r) {
  l = dfn[l], r = dfn[r];
  if (l > r) swap(l, r);
  int k = lg[r - l + 1];
  return ys[min(eu[l][k], eu[r - (1 << k) + 1][k])];
}
int st[500005], tp;
void ins(int x) {
  if (!tp) {
    st[++tp] = x;
    return;
  }
  int f = lca(x, st[tp]);
  while (tp > 1 && dfn[f] < dfn[st[tp - 1]])
    sn[st[tp - 1]].emplace_back(st[tp]), --tp;
  if (dfn[f] < dfn[st[tp]]) sn[f].emplace_back(st[tp]), --tp;
  if (f != st[tp]) st[++tp] = f;
  st[++tp] = x;
}
int sz[500005];
void efs(int x) {
  if (!((int)sn[x].size()))
    sz[x] = 1;
  else
    sz[x] = 0;
  for (const auto &v : sn[x]) {
    efs(v);
    sz[x] += sz[v];
  }
}
long long val[500005];
void hfs(int x, int _fa, long long s) {
  s += (dep[x] - dep[_fa]) * sz[x];
  if (!((int)sn[x].size())) val[x] = val[fa[x]] + s - 1;
  for (const auto &v : sn[x]) hfs(v, x, s);
  sn[x].clear();
}
int root;
signed main() {
  in(n);
  for (register int i = 1; i <= n; ++i) {
    in(fa[i]);
    if (!fa[i])
      root = i;
    else
      sn[fa[i]].emplace_back(i);
  }
  dfs(root, 0);
  prework();
  for (register int o = 1; o <= mxd; ++o) {
    if (o != 1) hv[o].emplace_back(root);
    sort(hv[o].begin(), hv[o].end(),
         [](const int a, const int b) { return dfn[a] < dfn[b]; });
    tp = 0;
    for (const auto &v : hv[o]) ins(v);
    while (tp > 1) sn[st[tp - 1]].emplace_back(st[tp]), --tp;
    efs(root);
    hfs(root, 0, 0);
  }
  prt(val + 1, n);
  return 0;
}
}  // namespace i207M
signed main() {
  i207M::main();
  return 0;
}
