#include <bits/stdc++.h>
using namespace std;
const long long N = 5e5;
const long long t = 20;
long long ans[N], tin[N], tout[N], h[N];
long long predm[t][N];
vector<long long> g[N];
long long timer = 0;
long long dsu[N], rang[N];
long long left1[N], right1[N];
long long max(long long a, long long b) {
  if (a > b) {
    return a;
  }
  return b;
}
long long pred(long long a) {
  if (a == dsu[a]) return a;
  return dsu[a] = pred(dsu[a]);
}
void unite(long long a, long long b) {
  if (rang[a] < rang[b]) {
    swap(a, b);
  }
  dsu[b] = a;
  rang[a] += rang[b];
  left1[a] = min(left1[a], left1[b]);
  right1[a] = max(right1[a], right1[b]);
}
void dfs(long long v, long long p = -1) {
  tin[v] = timer++;
  for (long long i = 0; i < g[v].size(); i++) {
    long long to = g[v][i];
    if (to == p) {
      continue;
    }
    predm[0][to] = v;
    h[to] = h[v] + 1;
    dfs(to, v);
  }
  tout[v] = timer - 1;
}
bool pr(long long a, long long b) {
  return tin[a] <= tin[b] && tout[a] >= tout[b];
}
long long lca(long long a, long long b) {
  if (pr(a, b)) {
    return a;
  }
  for (long long j = t - 1; j >= 0; j--) {
    if (!pr(predm[j][a], b)) {
      a = predm[j][a];
    }
  }
  a = predm[0][a];
  return a;
}
void dfs1(long long v) {
  for (long long i = 0; i < g[v].size(); i++) {
    long long to = g[v][i];
    ans[to] += ans[v];
    dfs1(to);
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n;
  cin >> n;
  long long st = 0;
  for (long long i = 0; i < n; i++) {
    long long p;
    cin >> p;
    p--;
    if (p == -1) {
      st = i;
    } else {
      g[p].push_back(i);
    }
  }
  predm[0][st] = st;
  dfs(st);
  for (long long i = 1; i < t; i++) {
    for (long long j = 0; j < n; j++) {
      predm[i][j] = predm[i - 1][predm[i - 1][j]];
    }
  }
  vector<pair<long long, long long> > vec;
  for (long long i = 0; i < n; i++) {
    dsu[i] = i;
    rang[i] = 1;
    vec.push_back({h[i], i});
  }
  vector<bool> used(n, false);
  sort(vec.begin(), vec.end());
  for (long long i = 0; i < n; i++) {
    long long j = i;
    vector<long long> add;
    vector<pair<long long, long long> > s1;
    vector<pair<pair<long long, long long>, pair<long long, long long> > > p1;
    while (j < n && vec[j].first == vec[i].first) {
      long long ind = vec[j].second;
      ans[ind] += 1 + h[ind];
      s1.push_back({tin[ind], ind});
      j++;
    }
    sort(s1.begin(), s1.end());
    add.resize(s1.size() + 1, 0);
    for (long long k = 0; k < s1.size(); k++) {
      left1[s1[k].second] = k;
      right1[s1[k].second] = k;
    }
    for (long long k = 0; k < s1.size() - 1; k++) {
      long long v = lca(s1[k].second, s1[k + 1].second);
      p1.push_back({{h[v], v}, {s1[k].second, s1[k + 1].second}});
    }
    if (p1.size() != 0) {
      sort(p1.begin(), p1.end());
      for (long long k = p1.size() - 1; k >= 0; k--) {
        long long j = k;
        long long h1 = p1[j].first.first + 1;
        long long allsz = 0;
        while (j >= 0 && p1[j].first == p1[k].first) {
          long long a = pred(p1[j].second.first);
          long long b = pred(p1[j].second.second);
          if (!used[b]) {
            used[b] = true;
            allsz += rang[b];
          }
          if (!used[a]) {
            used[a] = true;
            allsz += rang[a];
          }
          j--;
        }
        j = k;
        while (j >= 0 && p1[j].first == p1[k].first) {
          long long a = pred(p1[j].second.first);
          long long b = pred(p1[j].second.second);
          used[a] = used[b] = false;
          j--;
        }
        j = k;
        while (j >= 0 && p1[j].first == p1[k].first) {
          long long a = pred(p1[j].second.first);
          long long b = pred(p1[j].second.second);
          if (!used[b]) {
            used[b] = true;
            add[left1[b]] += (allsz - rang[b]) * h1;
            add[right1[b] + 1] -= (allsz - rang[b]) * h1;
          }
          if (!used[a]) {
            used[a] = true;
            add[left1[a]] += (allsz - rang[a]) * h1;
            add[right1[a] + 1] -= (allsz - rang[a]) * h1;
          }
          j--;
        }
        j = k;
        while (j >= 0 && p1[j].first == p1[k].first) {
          long long a = pred(p1[j].second.first);
          long long b = pred(p1[j].second.second);
          used[a] = used[b] = false;
          unite(a, b);
          j--;
        }
        k = j + 1;
      }
    }
    long long sum = 0;
    for (long long k = 0; k < s1.size(); k++) {
      sum += add[k];
      ans[s1[k].second] += sum;
    }
    i = j - 1;
  }
  dfs1(st);
  for (long long i = 0; i < n; i++) {
    cout << ans[i] - h[i] - 1 << " ";
  }
  return 0;
}
