#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n, ans[maxn], sz[maxn];
vector<int> g[maxn];
map<int, int> mc, mp, mo;
void getsz(int u) {
  sz[u] = 1;
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    getsz(v);
    sz[u] += sz[v];
  }
  ans[u] = n - 1;
  ++mo[sz[u]];
}
void a1(map<int, int> &m, int k, int d) {
  m[k] += d;
  if (m[k] == 0) m.erase(k);
}
void add(int u, int d, int bi) {
  a1(mc, sz[u], d);
  a1(mo, sz[u], -d);
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    if (v == bi) continue;
    add(v, d, bi);
  }
}
void bs(map<int, int> &m, int l, int r, int mi, int off, int s) {
  if (mi == n - 1) return;
  int ma = r, an = ma;
  while (r >= l) {
    int mid = (l + r) / 2;
    auto it = m.lower_bound(ma - mid + off);
    if (it == m.end() || (it->first) + mi > mid + off) {
      l = mid + 1;
    } else {
      r = mid - 1;
      an = min(an, mid);
    }
  }
  ans[s] = min(ans[s], an);
}
void dfs(int u, int f, bool keep) {
  a1(mp, sz[u], 1);
  a1(mo, sz[u], -1);
  int ma = -1, sma = -1, mi = n - 1, mci = -1;
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    if (sz[v] > ma) {
      sma = ma;
      ma = sz[v];
      mci = v;
    } else
      sma = max(sma, sz[v]);
    mi = min(mi, sz[v]);
  }
  if (f) mi = min(mi, n - sz[u]);
  for (int i = 0; i < g[u].size(); i++)
    if (g[u][i] != mci) dfs(g[u][i], u, false);
  if (mci != -1) dfs(mci, u, true);
  if (ma >= n - sz[u]) {
    sma = max(sma, n - sz[u]);
    bs(mc, sma, ma, mi, 0, u);
  }
  add(u, 1, mci);
  a1(mp, sz[u], -1);
  a1(mo, sz[u], 1);
  if (ma <= n - sz[u]) {
    sma = ma;
    bs(mp, sma, n - sz[u], mi, sz[u], u);
    bs(mo, sma, n - sz[u], mi, 0, u);
  }
  if (!keep) add(u, -1, -1);
}
int main() {
  scanf("%d", &n);
  int root;
  for (int i = 0; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    if (u == 0)
      root = v;
    else
      g[u].push_back(v);
  }
  getsz(root);
  dfs(root, 0, true);
  for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
  return 0;
}
