#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline bool chkmin(T &a, const T &b) {     return a > b ? a = b, 1 : 0;   }   template <typename T>   inline bool chkmax(T &a, const T &b) {     return a < b ? a = b, 1 : 0;   }   const int oo = 0x3f3f3f3f;   const int maxn = 500100;   int n;   vector<int> chd[maxn + 5];   int fa[maxn + 5];   int dep[maxn + 5];   void dfs(int first) {     for (auto second : chd[first]) dep[second] = dep[first] + 1, dfs(second);   }   struct node {     node *c[2], *p;     int label;     long long sum;     int val;     int sz;     node() : p(NULL), label(0), sum(0), val(0), sz(1) { memset(c, 0, sizeof c); }     void update() {       sum = val;       sz = 1;       for (int i = (0), i_end_ = (2); i < i_end_; ++i)         if (c[i]) sum += c[i]->sum, sz += c[i]->sz;     }     void flag_label(int _label) {       label += _label;       val += _label;       sum += (long long)sz * _label;     }     void push_down() {       if (label) {         for (int i = (0), i_end_ = (2); i < i_end_; ++i)           if (c[i]) c[i]->flag_label(label);         label = 0;       }     }     int get_pos() {       if (!this || !p) return 2;       for (int i = (0), i_end_ = (2); i < i_end_; ++i)         if (p->c[i] == this) return i;       return 2;     }     bool is_root() { return get_pos() >= 2; }     void setc(node *u, const int &v) {       if (this && v < 2) c[v] = u;       if (u) u->p = this;     }     void rotate() {       node *_p = this->p;       bool mark = get_pos();       _p->p->setc(this, _p->get_pos());       _p->setc(c[mark ^ 1], mark);       setc(_p, mark ^ 1);       _p->update();     }     void relax() {       static node *tmp[maxn + 5];       int top = 0;       node *u = this;       while (!u->is_root()) tmp[top++] = u, u = u->p;       u->push_down();       while (top) tmp[--top]->push_down();     }     void splay() {       relax();       for (; !is_root(); rotate())         if (!p->is_root())           ((p->p->c[0] == p) == (p->c[0] == this) ? p : this)->rotate();       update();     }     node *access() {       node *u = this, *v = NULL;       for (; u; v = u, u = u->p) u->splay(), u->setc(v, 1), u->update();       splay();       return v;     }   };   node nd[maxn + 5];   vector<int> all[maxn + 5];   long long ans[maxn + 5];   int main() {     scanf( %d , &n);     for (int i = (0), i_end_ = (n); i < i_end_; ++i) scanf( %d , fa + i), --fa[i];     int rt = -1;     for (int i = (0), i_end_ = (n); i < i_end_; ++i)       if (!~fa[i])         rt = i;       else         chd[fa[i]].push_back(i);     dep[rt] = 0;     dfs(rt);     for (int i = (0), i_end_ = (n); i < i_end_; ++i) {       nd[i].val = 0;       if (~fa[i])         nd[i].p = nd + fa[i];       else         nd[i].p = NULL;     }     for (int i = (0), i_end_ = (n); i < i_end_; ++i) all[dep[i]].push_back(i);     for (int i = (0), i_end_ = (n); i < i_end_; ++i) {       for (auto first : all[i]) {         node *u = nd + first;         u->access();         u->flag_label(1);       }       for (auto first : all[i]) {         node *u = nd + first;         u->access();         ans[first] = u->sum - dep[first] - 1;       }     }     for (int i = (0), i_end_ = (n); i < i_end_; ++i)       printf(            %             ll             d  ,           ans[i]);     return 0;   }