#include <bits/stdc++.h>
using namespace std;
struct node {
  int y, nex;
} a[110000];
struct node1 {
  int lc, rc, cnt;
} lt[110000 * 30];
int mx[110000][2], mi[110000], son[110000], n, len, fir[110000], dfn[110000],
    las[110000], id, siz[110000], rot;
int fa[110000], cnt[110000], r[110000], rt[110000], ans[110000], tl;
vector<int> v[110000];
multiset<int> s;
multiset<int>::iterator it;
void ins(int x, int y) {
  a[++len].y = y;
  a[len].nex = fir[x];
  fir[x] = len;
}
void dfs(int x) {
  dfn[x] = ++id;
  r[id] = x;
  siz[x] = 1;
  for (int k = fir[x]; k; k = a[k].nex) {
    int y = a[k].y;
    fa[y] = x;
    dfs(y);
    siz[x] += siz[y];
    if (siz[y] > mx[x][0]) {
      mx[x][1] = mx[x][0];
      mx[x][0] = siz[y];
    } else if (siz[y] > mx[x][1])
      mx[x][1] = siz[y];
    if (mi[x] == 0 || siz[y] < mi[x]) mi[x] = siz[y];
    cnt[x]++;
  }
  if (fa[x]) {
    if (n - siz[x] > mx[x][0]) {
      mx[x][1] = mx[x][0];
      mx[x][0] = n - siz[x];
    } else if (n - siz[x] > mx[x][1])
      mx[x][1] = n - siz[x];
    if (mi[x] == 0 || n - siz[x] < mi[x]) mi[x] = n - siz[x];
    cnt[x]++;
  }
  for (int k = fir[x]; k; k = a[k].nex) {
    int y = a[k].y;
    if (siz[y] == mx[x][0]) v[x].push_back(y);
  }
  if (fa[x] && n - siz[x] == mx[x][0]) v[x].push_back(fa[x]);
  las[x] = id;
}
void build(int &x, int y, int k, int l, int r) {
  x = ++tl;
  lt[x] = lt[y];
  lt[x].cnt++;
  if (l == r) return;
  int mid = (l + r) / 2;
  if (mid >= k)
    build(lt[x].lc, lt[y].lc, k, l, mid);
  else
    build(lt[x].rc, lt[y].rc, k, mid + 1, r);
}
void change(int &x, int k, int o, int l, int r) {
  if (x == 0) x = ++tl;
  lt[x].cnt += o;
  if (l == r) return;
  int mid = (l + r) / 2;
  if (mid >= k)
    change(lt[x].lc, k, o, l, mid);
  else
    change(lt[x].rc, k, o, mid + 1, r);
}
void upd(int x, int k) {
  ans[x] = min(ans[x], max(max(mx[x][0] - k, mi[x] + k), mx[x][1]));
}
int get_mx(int x1, int x2, int x3, int y, int l, int r, int ll, int rr) {
  int mid = (ll + rr) / 2;
  if (lt[x1].cnt + lt[x2].cnt + lt[x3].cnt - lt[y].cnt == 0) return 0;
  if (ll == rr) return ll;
  if (mid >= r)
    return get_mx(lt[x1].lc, lt[x2].lc, lt[x3].lc, lt[y].lc, l, r, ll, mid);
  else if (l > mid)
    return get_mx(lt[x1].rc, lt[x2].rc, lt[x3].rc, lt[y].rc, l, r, mid + 1, rr);
  else {
    int t = get_mx(lt[x1].rc, lt[x2].rc, lt[x3].rc, lt[y].rc, mid + 1, r,
                   mid + 1, rr);
    if (t == 0)
      t = get_mx(lt[x1].lc, lt[x2].lc, lt[x3].lc, lt[y].lc, l, mid, ll, mid);
    return t;
  }
}
int get_mi(int x1, int x2, int x3, int y, int l, int r, int ll, int rr) {
  int mid = (ll + rr) / 2;
  if (lt[x1].cnt + lt[x2].cnt + lt[x3].cnt - lt[y].cnt == 0) return 0;
  if (ll == rr) return ll;
  if (mid >= r)
    return get_mi(lt[x1].lc, lt[x2].lc, lt[x3].lc, lt[y].lc, l, r, ll, mid);
  else if (l > mid)
    return get_mi(lt[x1].rc, lt[x2].rc, lt[x3].rc, lt[y].rc, l, r, mid + 1, rr);
  else {
    int t = get_mi(lt[x1].lc, lt[x2].lc, lt[x3].lc, lt[y].lc, l, mid, ll, mid);
    if (t == 0)
      t = get_mi(lt[x1].rc, lt[x2].rc, lt[x3].rc, lt[y].rc, mid + 1, r, mid + 1,
                 rr);
    return t;
  }
}
void dp(int x) {
  if (x == 2) {
    int oo = 1;
  }
  ans[x] = mx[x][0];
  if (cnt[x] > 1) {
    int tot = v[x].size();
    for (int i = 0; i < tot; i++) {
      int y = v[x][i], d = min(mx[x][0] - mx[x][1], (mx[x][0] - mi[x]) / 2);
      if (y == fa[x]) {
        int k =
            get_mx(rt[n], rt[dfn[x] - 1], rt[n + 1], rt[las[x]], 1, d, 1, n);
        upd(x, k);
        k = get_mi(rt[n], rt[dfn[x] - 1], rt[n + 1], rt[las[x]], d, n, 1, n);
        upd(x, k);
        it = s.lower_bound(d + siz[x]);
        if (it != s.end()) {
          k = (*it) - siz[x];
          upd(x, k);
        }
        if (it != s.begin()) {
          it--;
          k = (*it) - siz[x];
          upd(x, k);
        }
      } else {
        int k = get_mx(rt[las[y]], 0, 0, rt[dfn[y] - 1], 1, d, 1, n);
        upd(x, k);
        k = get_mi(rt[las[y]], 0, 0, rt[dfn[y] - 1], d, n, 1, n);
        upd(x, k);
      }
    }
  }
  change(rt[n + 1], siz[x], -1, 1, n);
  s.insert(siz[x]);
  for (int k = fir[x]; k; k = a[k].nex) {
    int y = a[k].y;
    dp(y);
  }
  it = s.lower_bound(siz[x]);
  s.erase(it);
  change(rt[n + 1], siz[x], 1, 1, n);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (x == 0)
      rot = y;
    else
      ins(x, y);
  }
  dfs(rot);
  for (int i = 1; i <= n; i++) build(rt[i], rt[i - 1], siz[r[i]], 1, n);
  dp(rot);
  for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
  return 0;
}
