#include #include #include #include #include #include #include #include #include #include #include #include #define D int #define U unsigned using namespace std; D n, m,a, b, d, e, x, y, z; D i, j, k; struct Li { int n; int p; int r; int l; bool leaf; bool operator<(const Li &obj) const{ return n < obj.n; } }; Li tr[100000]; int main() { int t,u; cin >> a; tr[0].n = 0; tr[0].p = -1; tr[0].r = 0; tr[0].leaf = true; for (i = 1; i < a; i++) { cin >> tr[i].p >> tr[i].n; tr[i].p--; tr[i].n--; } sort(tr, tr + a); for (i = 1; i < a; i++) { tr[i].r = tr[tr[i].p].r + 1; if(tr[tr[i].p].leaf) tr[tr[i].p].leaf = false; tr[i].leaf = true; } for (i = 0; i < a; i++) { if (tr[i].leaf) { tr[i].l = 0; t = tr[i].p; u = i; while (t>=0) { tr[t].l = tr[u].l + 1; u = t; t = tr[t].p; } } } for (i = 0; i < a;i++) { cout << min(tr[i].r, tr[i].l) << endl; } }