#include <bits/stdc++.h>
using namespace std;
const int DIM = 1e5 + 5;
int szt[DIM], ans[DIM];
vector<int> edg[DIM];
map<int, int> son, par, out;
void dfs1(int x, int sz) {
  szt[x] = 1;
  for (int y : edg[x]) {
    dfs1(y, sz);
    szt[x] += szt[y];
  }
  out[szt[x]]++;
  ans[x] = sz - 1;
  return;
}
void add(map<int, int> &mmp, int x, int val) {
  mmp[x] += val;
  out[x] -= val;
  if (!mmp[x]) mmp.erase(x);
  if (!out[x]) out.erase(x);
  return;
}
void bns(map<int, int> &mmp, int x, int le, int ri, int mnm, int aux) {
  int val = ri, sol = ri;
  while (le <= ri) {
    int md = (le + ri) >> 1;
    map<int, int>::iterator it = mmp.lower_bound(val - md + aux);
    if (it == mmp.end() || it->first + mnm > md + aux)
      le = md + 1;
    else
      ri = md - 1, sol = min(sol, md);
  }
  ans[x] = min(ans[x], sol);
  return;
}
void dfs3(int x, int val, int pos) {
  add(son, szt[x], val);
  for (int y : edg[x])
    if (y != pos) dfs3(y, val, pos);
  return;
}
void dfs2(int x, int sz, bool oki) {
  int mxm1(-1), mxm2(-1), pos(-1), mnm(sz - 1);
  add(par, szt[x], 1);
  for (int y : edg[x]) {
    mnm = min(mnm, szt[y]);
    if (mxm1 < szt[y])
      mxm2 = mxm1, mxm1 = szt[y], pos = y;
    else
      mxm2 = max(mxm2, szt[y]);
  }
  if (szt[x] != sz) mnm = min(mnm, sz - szt[x]);
  for (int y : edg[x])
    if (y != pos) dfs2(y, sz, false);
  if (pos != -1) dfs2(pos, sz, true);
  if (mxm1 >= sz - szt[x]) {
    mxm2 = max(mxm2, sz - szt[x]);
    if (mnm != sz - 1) bns(son, x, mxm2, mxm1, mnm, 0);
  }
  dfs3(x, 1, pos);
  add(par, szt[x], -1);
  if (mxm1 <= sz - szt[x] && mnm != sz - 1) {
    bns(par, x, mxm1, sz - szt[x], mnm, szt[x]);
    bns(out, x, mxm1, sz - szt[x], mnm, 0);
  }
  if (!oki) dfs3(x, -1, -1);
  return;
}
int main(void) {
  int n, r(-1);
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    int x, y;
    scanf("%d %d", &x, &y);
    if (x == 0)
      r = y;
    else
      edg[x].push_back(y);
  }
  dfs1(r, n);
  dfs2(r, n, true);
  for (int i = 1; i <= n; ++i) printf("%d\n", ans[i]);
  return 0;
}
