#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int data = 0, w = 1;
  char ch = 0;
  while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
  if (ch == '-') w = -1, ch = getchar();
  while (ch >= '0' && ch <= '9') data = data * 10 + ch - '0', ch = getchar();
  return data * w;
}
int n, h[1000005], cnt, p[500005][19], X, dep[1000005], pre[1000005], Md,
    dfn[1000005];
long long z[1000005];
vector<int> ve[500005];
struct node {
  int to, next;
} w[1000005];
void add(int x, int y) {
  if (!x) return void(X = y);
  w[++cnt] = (node){y, h[x]};
  h[x] = cnt;
}
void dfs(int x) {
  Md = max(Md, dep[x]);
  dfn[x] = ++dfn[0];
  ve[dep[x]].push_back(x);
  for (int i = 1; i <= 18; i++) p[x][i] = p[p[x][i - 1]][i - 1];
  for (int i = h[x]; i; i = w[i].next) {
    int y = w[i].to;
    dep[y] = dep[x] + 1;
    p[y][0] = x;
    dfs(y);
  }
}
void dfsans(int x) {
  for (int i = h[x]; i; i = w[i].next)
    z[w[i].to] += z[x] + dep[x], dfsans(w[i].to);
}
int lca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  for (int i = 18; i >= 0; i--)
    if (dep[p[x][i]] >= dep[y]) x = p[x][i];
  if (x == y) return x;
  for (int i = 18; i >= 0; i--)
    if (p[x][i] != p[y][i]) x = p[x][i], y = p[y][i];
  return p[x][0];
}
int st[1000005], v[1000005];
void Solve(vector<int> P) {
  int S = P.size() - 1;
  for (int i = 1; i <= S; i++) pre[i] = dep[lca(P[i], P[i - 1])];
  long long now = 0, top = 0;
  st[0] = -1;
  for (int i = 1; i <= S; i++) {
    while (top && pre[i] <= v[top])
      now += 1ll * (pre[i] - v[top]) * (st[top] - st[top - 1]), --top;
    st[++top] = i - 1, v[top] = pre[i];
    now += pre[i];
    z[P[i]] += now;
  }
  reverse(P.begin(), P.end());
  top = now = 0;
  reverse(pre + 1, pre + S + 1);
  for (int i = 1; i <= S; i++) {
    while (top && pre[i] <= v[top])
      now += 1ll * (pre[i] - v[top]) * (st[top] - st[top - 1]), --top;
    st[++top] = i - 1, v[top] = pre[i];
    now += pre[i];
    z[P[i]] += now;
  }
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) add(read(), i);
  dep[X] = 1;
  dfs(X);
  for (int i = 1; i <= Md; i++)
    sort(ve[i].begin(), ve[i].end(),
         [](int x, int y) { return dfn[x] < dfn[y]; }),
        Solve(ve[i]);
  dfsans(X);
  for (int i = 1; i <= n; i++) cout << z[i] << ' ';
  return 0;
}
