#include <bits/stdc++.h>
using namespace std;
const int maxn = 100100, maxv = 110, mod = 1e9 + 7, maxa = 1005, maxs = 820,
          maxb = 33, base = 737, base2 = 3079, mod3 = 1e7 + 19;
const long long inf = 2e14, infint = 1e9 + 11;
vector<int> v[maxn];
int mn[maxn];
vector<int> mx[maxn];
int sz[maxn];
bool visited[maxn];
bool mark[maxn];
int n;
set<pair<int, int> > parents;
set<pair<int, int> > others;
set<int> children[maxn];
int ans[maxn];
inline void ok(int x) {
  sort(mx[x].begin(), mx[x].end());
  reverse(mx[x].begin(), mx[x].end());
  while (mx[x].size() > 2) {
    mx[x].pop_back();
  }
}
inline void plant(int x) {
  sz[x] = 1;
  for (auto u : v[x]) {
    plant(u);
    sz[x] += sz[u];
    mn[x] = min(mn[x], sz[u]);
    mx[x].push_back(sz[u]);
    ok(x);
  }
  if (n - sz[x]) {
    mn[x] = min(mn[x], n - sz[x]);
    mx[x].push_back(n - sz[x]);
  }
  ok(x);
  while (mx[x].size() < 2) {
    mx[x].push_back(0);
  }
}
inline int LeftAppKid(int u, int s) {
  auto it = children[u].upper_bound(s);
  if (it == children[u].begin()) {
    return 0;
  }
  it--;
  int res = *it;
  return res;
}
inline int RightAppKid(int u, int s) {
  auto it = children[u].upper_bound(s);
  if (it == children[u].end()) {
    return infint;
  }
  int res = *it;
  return res;
}
inline int LeftAppPar(int s) {
  auto it = parents.upper_bound(make_pair(s, infint));
  if (it == parents.begin()) {
    return 0;
  }
  it--;
  int res = it->first;
  return res;
}
inline int RightAppPar(int s) {
  auto it = parents.upper_bound(make_pair(s, infint));
  if (it == parents.end()) {
    return infint;
  }
  int res = it->first;
  return res;
}
inline int LeftAppOthers(int s) {
  auto it = others.upper_bound(make_pair(s, infint));
  if (it == others.begin()) {
    return 0;
  }
  it--;
  int res = it->first;
  return res;
}
inline int RightAppOthers(int s) {
  auto it = others.upper_bound(make_pair(s, infint));
  if (it == others.end()) {
    return infint;
  }
  int res = it->first;
  return res;
}
inline void SolveChild(int x, int u) {
  int target = (mx[x][0] - mn[x]) / 2;
  int lapp = LeftAppKid(u, target);
  int rapp = RightAppKid(u, target);
  ans[x] = min(
      ans[x], min(max(mx[x][1], mx[x][0] - lapp), max(mx[x][1], mn[x] + rapp)));
}
inline void SolvePar(int x) {
  int target = (mx[x][0] - mn[x]) / 2;
  int lappP = LeftAppPar(target + sz[x]);
  int rappP = RightAppPar(target + sz[x]);
  int lappO = LeftAppOthers(target);
  int rappO = RightAppOthers(target);
  ans[x] = min(ans[x], min(max(mx[x][1], mx[x][0] - lappP + sz[x]),
                           max(mx[x][1], mn[x] + rappP - sz[x])));
  ans[x] = min(ans[x], min(max(mx[x][1], mx[x][0] - lappO),
                           max(mx[x][1], mn[x] + rappO)));
}
inline void unite(int x, int y) {
  for (auto u : children[y]) {
    children[x].insert(u);
  }
}
inline void water(int x) {
  int boss = x;
  for (auto u : v[x]) {
    water(u);
    if (children[u].size() > children[boss].size()) {
      boss = u;
    }
    if (sz[u] == mx[x][0]) {
      SolveChild(x, u);
    }
  }
  children[boss].swap(children[x]);
  for (auto u : v[x]) {
    unite(x, u);
    children[u].clear();
  }
  children[x].insert(sz[x]);
}
inline void grow(int x) {
  if (n - sz[x] == mx[x][0]) {
    SolvePar(x);
  }
  parents.insert(make_pair(sz[x], x));
  for (auto u : v[x]) {
    grow(u);
  }
  parents.erase(make_pair(sz[x], x));
  others.insert(make_pair(sz[x], x));
}
inline void EatFruit() {
  for (int i = 0; i < n; i++) {
    reverse(v[i].begin(), v[i].end());
  }
  parents.clear();
  others.clear();
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  fill(mn, mn + maxn, infint);
  fill(ans, ans + maxn, infint);
  int rt = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (a + 1) {
      v[a].push_back(b);
      continue;
    }
    rt = b;
  }
  plant(rt);
  water(rt);
  grow(rt);
  EatFruit();
  grow(rt);
  for (int i = 0; i < n; i++) {
    cout << ans[i] << '\n';
  }
  cout << endl;
  return 0;
}
