#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug& operator<<(const c&) {
    return *this;
  }
};
using ll = long long;
using ld = long double;
constexpr int nax = 1000 * 1000 + 105;
constexpr int infty = 1000 * 1000 * 1000 + 5;
constexpr int mod = 1000 * 1000 * 1000 + 7;
struct Value {
  Value(ll val) : rozmiar(1), wart(val), spu(0), sum(val) {}
  void touch(Value* left, Value* right) {
    if (spu) {
      wart += spu;
      sum += spu * rozmiar;
      if (left) left->spu += spu;
      if (right) right->spu += spu;
      spu = 0;
    }
  }
  void update(Value* left, Value* right) {
    assert(spu == 0);
    sum = wart;
    rozmiar = 1;
    if (left) {
      sum += left->sum;
      rozmiar += left->rozmiar;
    }
    if (right) {
      sum += right->sum;
      rozmiar += right->rozmiar;
    }
  }
  int rozmiar;
  ll wart;
  ll spu;
  ll sum;
};
struct Splay : Value {
  Splay *l, *r, *p;
  bool flip;
  template <typename... Args>
  Splay(const Args&... args)
      : Value(args...), l(nullptr), r(nullptr), p(nullptr), flip(false) {}
  bool isRoot() { return !p || (p->l != this && p->r != this); }
  void touch() {
    if (flip) {
      swap(l, r);
      if (l) l->flip = !l->flip;
      if (r) r->flip = !r->flip;
      flip = false;
    }
    Value::touch(l, r);
  }
  void update() { Value::update(l, r); }
  static void connect(Splay* p, Splay* c, bool left) {
    (left ? p->l : p->r) = c;
    if (c) c->p = p;
  }
  void rotate() {
    Splay* f = p;
    Splay* t = f->p;
    f->touch();
    touch();
    bool isr = f->isRoot();
    bool left = (this == f->l);
    connect(f, left ? r : l, left);
    connect(this, f, !left);
    if (isr)
      this->p = t;
    else
      connect(t, this, f == t->l);
    if (f->r) f->r->touch();
    if (f->l) f->l->touch();
    f->update();
  }
  void splay() {
    while (!isRoot()) {
      Splay* r = p->p;
      if (!p->isRoot()) (((p->l == this) == (r->l == p)) ? p : this)->rotate();
      rotate();
    }
    touch();
    if (l) l->touch();
    if (r) r->touch();
    update();
  }
  static Splay* lastDisc;
  void expose() {
    Splay* r = nullptr;
    lastDisc = nullptr;
    for (Splay* q = this; q; q = q->p) {
      q->splay();
      q->r = r;
      q->update();
      r = q;
      lastDisc = q;
    }
    splay();
  }
  void link(Splay* to) {
    expose();
    assert(l == nullptr);
    p = to;
  }
  Splay* root() {
    expose();
    Splay* s = this;
    while (s->l) {
      s = s->l;
      s->touch();
    }
    s->splay();
    return s;
  }
  void applyF(std::function<void(Splay*)> f) {
    expose();
    if (r == nullptr)
      f(this);
    else {
      Splay* s = r;
      while (s->l) {
        s = s->l;
        s->touch();
      }
      s->splay();
      f(s->l);
    }
  }
  void makeRoot() {
    applyF([](Splay* s) { s->flip = !s->flip; });
  }
  void cut() {
    expose();
    assert(l != nullptr);
    Splay* s = l;
    while (s->r) s = s->r;
    s->splay();
    s->r->p = nullptr;
    s->r = nullptr;
  }
  Splay* lca(Splay* v) {
    expose();
    v->expose();
    return lastDisc;
  }
};
Splay* Splay::lastDisc;
void applyF(Splay* a, Splay* b, std::function<void(Splay*)> f) {
  a->makeRoot();
  b->applyF(f);
}
int nast = 0;
template <typename... Args>
Splay* newTree(const Args&... args) {
  return new Splay(args...);
}
void deleteTree(Splay* t) {
  if (t->l) deleteTree(t->l);
  if (t->r) deleteTree(t->r);
  delete t;
}
int n;
Splay* wierz[nax];
int root;
vector<int> drzewo[nax];
int roz[nax];
vector<int> gleb[nax];
void LiczPoddrzewa(int w, int g) {
  gleb[g].push_back(w);
  for (int d : drzewo[w]) {
    LiczPoddrzewa(d, g + 1);
    roz[w] += 1 + roz[d];
  }
  wierz[w] = newTree(roz[w]);
  for (int d : drzewo[w]) {
    wierz[d]->link(wierz[w]);
  }
}
ll odp[nax];
void LiczOdp(int w) {
  applyF(wierz[w], wierz[root], [w](Splay* s) -> void {
    assert(s != nullptr);
    assert(s->spu == 0);
    odp[w] = s->sum;
  });
}
void Wywal(int w) {
  applyF(wierz[w], wierz[root], [](Splay* s) -> void {
    assert(s != nullptr);
    s->spu--;
  });
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int p;
    scanf("%d", &p);
    if (p == 0) {
      root = i;
    } else {
      drzewo[p].push_back(i);
    }
  }
  LiczPoddrzewa(root, 0);
  for (int g = n; g >= 0; g--) {
    for (int w : gleb[g]) {
      LiczOdp(w);
    }
    for (int w : gleb[g]) {
      Wywal(w);
    }
  }
  for (int i = 1; i <= n; i++) {
    printf("%lld ", odp[i]);
  }
  printf("\n");
  return 0;
}
