#include <bits/stdc++.h>
using namespace std;
const long long oo = 0x3f3f3f3f3f3f3f3fll;
const int LGN = 25;
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
int n;
vector<int> T[N];
int ans[N];
int sub[N], l[N], r[N], t, vx[N];
multiset<int> so, sa;
void dfs0(int u) {
  sub[u] = 1;
  l[u] = ++t;
  vx[t] = u;
  for (int v : T[u]) dfs0(v), sub[u] += sub[v];
  sort((T[u]).begin(), (T[u]).end(),
       [](int a, int b) { return sub[a] > sub[b]; });
  so.insert(sub[u]);
  r[u] = t;
}
vector<int> st[4 * N];
void build(int p = 1, int L = 1, int R = t) {
  if (L == R) {
    st[p].push_back(sub[vx[L]]);
    return;
  }
  int m = (L + R) >> 1;
  build(2 * p, L, m), build(2 * p + 1, m + 1, R);
  st[p] = vector<int>(R - L + 1);
  merge((st[2 * p]).begin(), (st[2 * p]).end(), (st[2 * p + 1]).begin(),
        (st[2 * p + 1]).end(), st[p].begin());
}
void find(int l, int r, int v, function<void(int)> foo, int p = 1, int L = 1,
          int R = t) {
  if (R < L || r < L || R < l) return;
  if (l <= L && R <= r) {
    int i = lower_bound((st[p]).begin(), (st[p]).end(), v) - st[p].begin();
    for (long long j = max(i - 3, 0), __R = min((int)st[p].size() - 1, i + 3);
         j <= __R; j++)
      foo(st[p][j]);
    return;
  }
  int m = (L + R) >> 1;
  find(l, r, v, foo, 2 * p, L, m), find(l, r, v, foo, 2 * p + 1, m + 1, R);
}
void add(int u) { so.erase(so.find(sub[u])); }
void dfsadd(int u) {
  add(u);
  for (int v : T[u]) dfsadd(v);
}
void rem(int u) { so.insert(sub[u]); }
void dfsrem(int u) {
  rem(u);
  for (int v : T[u]) dfsrem(v);
}
void answer(int u) {
  if (sub[u] == 1) {
    ans[u] = n - 1;
    return;
  }
  int sz = so.size() + sa.size();
  int mi = sub[T[u][T[u].size() - 1]];
  if (sz) mi = min(mi, sz);
  int ma = sub[T[u][0]];
  auto relaxx = [&](int x) { ans[u] = min(ans[u], max(ma - x, mi + x)); };
  auto relax = [&](multiset<int>& st, int z, int off) {
    auto it = st.upper_bound(z + off);
    if (it != st.begin()) it--;
    for (long long i = 1, __R = 2; i <= __R; i++)
      if (it != st.end()) {
        int val = (*it) - off;
        ans[u] = min(ans[u], max(ma - val, mi + val));
        it++;
      }
  };
  if (sz <= ma) {
    ans[u] = ma;
    if (sz < ma && (T[u].size() == 1 || sub[T[u][1]] < ma)) {
      find(l[T[u][0]] + 1, r[T[u][0]], (ma - mi) >> 1, relaxx);
    }
  } else {
    ma = sz;
    ans[u] = ma;
    int z = (ma - mi) >> 1;
    relax(so, z, 0);
    relax(sa, z, sub[u]);
  }
  if (mi < sz && sz < ma) ans[u] = max(ans[u], sz);
  for (int v : T[u])
    if (mi < sub[v] && sub[v] < ma) {
      ans[u] = max(ans[u], sub[v]);
    }
}
void dfs(int u, bool keep = false) {
  if (T[u].size() == 0) {
    answer(u);
    if (keep) add(u);
    return;
  }
  add(u);
  sa.insert(sub[u]);
  for (long long i = 1, __R = T[u].size() - 1; i <= __R; i++) dfs(T[u][i]);
  dfs(T[u][0], true);
  sa.erase(sa.find(sub[u]));
  for (long long i = 1, __R = T[u].size() - 1; i <= __R; i++) dfsadd(T[u][i]);
  answer(u);
  if (!keep) dfsrem(u);
}
int main() {
  std::ios::sync_with_stdio(false);
  scanf("%d", &(n));
  int root;
  for (long long i = 1, __R = n; i <= __R; i++) {
    int u, v;
    scanf("%d%d", &(u), &(v));
    T[u].push_back(v);
    if (!u) root = v;
  }
  dfs0(root);
  build();
  dfs(root);
  for (long long i = 1, __R = n; i <= __R; i++) printf("%d\n", ans[i]);
  return 0;
}
