#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
int n, rt, fc[20], Log[N];
long long ans[N];
int read() {
  int ret = 0;
  char c = getchar();
  while (!isdigit(c)) c = getchar();
  while (isdigit(c)) ret = ret * 10 + (c ^ 48), c = getchar();
  return ret;
}
void write(long long x) {
  if (x > 9) write(x / 10);
  putchar(x % 10 ^ 48);
}
void writesp(long long x) {
  write(x);
  putchar(' ');
}
int tot, ind, head[N], dep[N], pos[N], fa[20][N];
vector<int> st[N];
struct Tway {
  int v, nex;
};
Tway e[N << 1];
void add(int u, int v) {
  e[++tot] = (Tway){v, head[u]};
  head[u] = tot;
  e[++tot] = (Tway){u, head[v]};
  head[v] = tot;
}
void dfs(int x) {
  pos[x] = ++ind;
  for (int i = 1; fc[i] <= dep[x]; i++) fa[i][x] = fa[i - 1][fa[i - 1][x]];
  for (int i = head[x]; i; i = e[i].nex) {
    int v = e[i].v;
    if (v == fa[0][x]) continue;
    dep[v] = dep[x] + 1;
    fa[0][v] = x;
    st[dep[v]].push_back(v);
    dfs(v);
  }
}
int lca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  for (int i = 0, t = dep[x] - dep[y]; i < 20; ++i)
    if (t & fc[i]) x = fa[i][x];
  for (int i = 19; ~i; --i)
    if (fa[i][x] ^ fa[i][y]) {
      x = fa[i][x];
      y = fa[i][y];
    }
  return x == y ? x : fa[0][x];
}
struct Stack {
  int len, a[N], b[N], c[N];
  void clear() { len = 0; }
  void push(int x, int y, int z) {
    len++;
    a[len] = x;
    b[len] = y;
    c[len] = z;
  }
  void pop() { --len; }
  int topa() { return a[len]; }
  int topb() { return b[len]; }
  long long calctop() { return (long long)b[len] * (c[len] - c[len - 1]); }
};
Stack S;
void initsomething() {
  fc[0] = 1;
  for (int i = 1; i < 20; ++i) fc[i] = fc[i - 1] << 1;
  for (int i = 2; i < N; ++i) Log[i] = Log[i >> 1] + 1;
}
void calc(const vector<int> &a) {
  long long res = 0;
  S.clear();
  for (int i = 0; i < (int)a.size(); i++) {
    int x = a[i];
    if (!i)
      S.push(x, 0, 0);
    else {
      for (;;) {
        int y = lca(S.topa(), x);
        if (S.topb() <= dep[y]) {
          S.push(x, dep[y] + 1, i);
          break;
        }
        res -= S.calctop();
        S.pop();
      }
      res += S.calctop();
    }
    ans[x] += res;
  }
}
int main() {
  n = read();
  for (int i = 1, x; i <= n; ++i) {
    x = read();
    if (x)
      add(i, x);
    else
      rt = i;
  }
  initsomething();
  dfs(rt);
  for (int d = 1; d <= n; ++d) {
    sort(st[d].begin(), st[d].end(),
         [=](int x, int y) -> bool { return pos[x] < pos[y]; });
    for (int v : st[d]) ans[v] += ans[fa[0][v]] + d;
    calc(st[d]);
    reverse(st[d].begin(), st[d].end());
    calc(st[d]);
  }
  for (int i = 1; i <= n; ++i) writesp(ans[i]);
  return 0;
}
