#include <bits/stdc++.h>
using namespace std;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1e9 + 100;
const long long LINF = 1e18 + 100;
const int MAXN = 100100;
int n;
vector<int> cld[MAXN];
int par[MAXN];
int rt;
int sttime[MAXN], fntime[MAXN], timer, revst[MAXN];
int subtree[MAXN];
int ans[MAXN];
multiset<int> seg[4 * MAXN];
multiset<int> rpath;
int rpadd;
int findcls(multiset<int> &ms, int lz, int x) {
  int res = INF;
  auto it = ms.lower_bound((x + 1) / 2 - lz);
  if (it != ms.end() && (res == INF || abs(2 * (*it) - (x - 2 * lz)) <
                                           abs(2 * res - (x - 2 * lz))))
    res = *it;
  if (it == ms.begin()) return res + lz;
  it--;
  if (res == INF || abs(2 * (*it) - (x - 2 * lz)) < abs(2 * res - (x - 2 * lz)))
    res = *it;
  return res + lz;
}
void build(int v = 1, int l = 0, int r = n - 1) {
  if (l == r) {
    seg[v].insert(subtree[revst[l]]);
    return;
  }
  int mid = (r + l) / 2;
  build(2 * v, l, mid);
  build(2 * v + 1, mid + 1, r);
  seg[v] = seg[2 * v];
  for (int u : seg[2 * v + 1]) seg[v].insert(u);
}
void setind(int i, int old, int nw, int v = 1, int l = 0, int r = n - 1) {
  seg[v].erase(seg[v].find(old));
  seg[v].insert(nw);
  if (l == r) return;
  int mid = (r + l) / 2;
  if (i <= mid)
    setind(i, old, nw, 2 * v, l, mid);
  else
    setind(i, old, nw, 2 * v + 1, mid + 1, r);
}
int get(int L, int R, int x, int v = 1, int l = 0, int r = n - 1) {
  if (l > R || r < L) return INF;
  if (l >= L && r <= R) return findcls(seg[v], 0, x);
  int mid = (r + l) / 2;
  int s1 = get(L, R, x, 2 * v, l, mid);
  int s2 = get(L, R, x, 2 * v + 1, mid + 1, r);
  if (abs(2 * s2 - x) < abs(2 * s1 - x)) return s2;
  return s1;
}
void dfs1(int v) {
  sttime[v] = timer++;
  revst[sttime[v]] = v;
  subtree[v] = 1;
  for (int u : cld[v]) dfs1(u), subtree[v] += subtree[u];
  fntime[v] = timer;
}
void dfs2(int v) {
  if (cld[v].empty()) {
    ans[v] = n - 1;
    return;
  }
  int mxv = rt;
  int mx = n - subtree[v], mx2 = 0, mn = (v == rt ? INF : n - subtree[v]);
  for (int u : cld[v]) {
    mn = min(mn, subtree[u]);
    if (subtree[u] >= mx)
      mxv = u, mx2 = mx, mx = subtree[u];
    else
      mx2 = max(mx2, subtree[u]);
  };
  ;
  ;
  ;
  ;
  ;
  ;
  ;
  ;
  ;
  ;
  ;
  if (mxv != rt) {
    int sub = get(sttime[mxv], fntime[mxv] - 1, mx - mn);
    ans[v] = max({mx2, mx - sub, mn + sub});
    ;
    ;
  } else {
    int s1 = get(0, sttime[v] - 1, mx - mn);
    int s2 = get(fntime[v], n - 1, mx - mn);
    int s3 = findcls(rpath, rpadd, mx - mn);
    ;
    ;
    ;
    ;
    ;
    ;
    if (abs(2 * s2 - (mx - mn)) < abs(2 * s1 - (mx - mn))) swap(s1, s2);
    if (abs(2 * s3 - (mx - mn)) < abs(2 * s1 - (mx - mn))) swap(s1, s3);
    ans[v] = max({mx2, mx - s1, mn + s1});
  }
  ans[v] = min(ans[v], mx);
  if (v == rt && cld[v].size() == 1) ans[v] = n - 1;
  setind(sttime[v], subtree[v], INF);
  rpath.insert(-rpadd);
  for (int u : cld[v]) rpadd += subtree[u];
  rpadd++;
  for (int u : cld[v]) {
    rpadd -= subtree[u];
    dfs2(u);
    rpadd += subtree[u];
  }
  for (int u : cld[v]) rpadd -= subtree[u];
  rpadd--;
  rpath.erase(rpath.find(-rpadd));
  setind(sttime[v], INF, subtree[v]);
}
int32_t main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n;
  for (int i = (1); i < (n + 1); i++) {
    int p, x;
    cin >> p >> x;
    if (p == 0)
      rt = x;
    else
      par[x] = p, cld[p].push_back(x);
  }
  dfs1(rt);
  build();
  ;
  ;
  ;
  ;
  ;
  ;
  dfs2(rt);
  for (int i = (1); i < (n + 1); i++) {
    cout << ans[i] << '\n';
  }
  return 0;
}
