#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O2,no-stack-protector,unroll-loops,fast-math")
const int maxn = 1e5 + 10, maxm = 1e6 + 10, lg = 17, mod = 1e9 + 7, inf = 1e18;
int n, rt, sz[maxn], st[maxn], ft[maxn], tt, ans[maxn];
vector<int> g[maxn];
multiset<int> seg[maxn << 2], R;
int kaf, saf;
int get(int l, int r, int s = 0, int t = n, int v = 1) {
  if (l >= t || r <= s) return maxn;
  if (l <= s && r >= t) {
    int ret = maxn;
    auto lb = seg[v].lower_bound(saf);
    if (lb != seg[v].end()) {
      ret = min(ret, (*lb) - saf);
    }
    auto ub = seg[v].upper_bound(kaf);
    if (ub != seg[v].begin()) {
      --ub;
      ret = min(ret, kaf - (*ub));
    }
    return ret;
  }
  return min(get(l, r, s, ((s + t) >> 1), (v << 1)),
             get(l, r, ((s + t) >> 1), t, ((v << 1) ^ 1)));
}
void upd(int i, bool b, int s = 0, int t = n, int v = 1) {
  if (b)
    seg[v].insert(sz[i]);
  else
    seg[v].erase(seg[v].find(sz[i]));
  if (t - s == 1) return;
  return (st[i] < ((s + t) >> 1)
              ? upd(i, b, s, ((s + t) >> 1), (v << 1))
              : upd(i, b, ((s + t) >> 1), t, ((v << 1) ^ 1)));
}
void pfs(int v = rt) {
  st[v] = tt++;
  sz[v] = 1;
  for (auto u : g[v]) pfs(u), sz[v] += sz[u];
  upd(v, 1);
  ft[v] = tt;
}
int ajdad(int sav) {
  kaf += sav, saf += sav;
  int ret = maxn;
  auto lb = R.lower_bound(saf);
  if (lb != R.end()) {
    ret = min(ret, (*lb) - saf);
  }
  auto ub = R.upper_bound(kaf);
  if (ub != R.begin()) {
    --ub;
    ret = min(ret, kaf - (*ub));
  }
  kaf -= sav, saf -= sav;
  return ret;
}
void dfs(int v = rt) {
  upd(v, 0);
  R.insert(sz[v]);
  for (auto u : g[v]) dfs(u);
  upd(v, 1);
  R.erase(R.find(sz[v]));
  vector<pair<int, int> > mol;
  if (v != rt) mol.push_back({n - sz[v], 0});
  for (auto u : g[v]) mol.push_back({sz[u], u});
  sort(mol.begin(), mol.end());
  if ((int)(mol.size()) == 1)
    ans[v] = mol[0].first;
  else {
    int mn = mol[0].first, mx = mol.back().first, sab = 0;
    sab = (mn + mx + 1) / 2;
    kaf = mx - sab, saf = sab - mn;
    int u = mol.back().second;
    if (u)
      ans[v] = get(st[u], ft[u]);
    else
      ans[v] = min({get(0, st[v]), get(ft[v], n), ajdad(sz[v])});
    ans[v] += sab;
    ans[v] = min(ans[v], mx);
    if ((int)(mol.size()) > 2)
      ans[v] = max(ans[v], mol[(int)(mol.size()) - 2].first);
  }
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int v, u;
    cin >> u >> v;
    if (u == 0)
      rt = v;
    else
      g[u].push_back(v);
  }
  if (n == 1) return cout << 0, 0;
  pfs(), dfs();
  for (int i = 1; i <= n; i++) cout << ans[i] << '\n';
  return 0;
}
