#include <bits/stdc++.h>
using namespace std;
int in() {
  int32_t x;
  scanf("%d", &x);
  return x;
}
const int maxn = 2e5 + 10;
const int inf = 1e9 + 10;
vector<int> g[maxn];
int n, rt, sz[maxn], heavy[maxn], ans[maxn];
map<int, int> up, father, child;
inline void ok(map<int, int> &mp, int x, int add) {
  if (mp[x] == 1 && add == -1)
    mp.erase(x);
  else
    mp[x] += add;
}
inline void pre_calc(int node) {
  sz[node] = 1;
  for (auto u : g[node]) {
    pre_calc(u), sz[node] += sz[u],
        heavy[node] =
            heavy[node] == -1 || sz[heavy[node]] < sz[u] ? u : heavy[node];
  }
  ok(up, sz[node], 1);
}
inline void Insert(int node) {
  ok(up, sz[node], -1);
  ok(child, sz[node], 1);
  for (auto u : g[node]) Insert(u);
}
inline void Clear(int node) {
  ok(up, sz[node], 1);
  ok(child, sz[node], -1);
  for (auto u : g[node]) Clear(u);
}
inline void binary_search(map<int, int> &mp, int low, int hi, int mini,
                          int node, int sub) {
  int maxi = hi;
  while (low + 1 < hi) {
    int mid = (low + hi) >> 1;
    auto it = mp.lower_bound(maxi - mid + sub);
    if (it == mp.end() || mini + it->first > mid + sub)
      low = mid;
    else
      hi = mid;
  }
  ans[node] = min(ans[node], hi);
}
inline void dfs(int node, bool cl = 0) {
  ok(up, sz[node], -1);
  ok(father, sz[node], 1);
  vector<pair<int, int> > subtrees;
  for (auto u : g[node]) subtrees.push_back({sz[u], u});
  if (node - rt) subtrees.push_back({sz[rt] - sz[node], rt});
  sort(subtrees.begin(), subtrees.end());
  int split = (int)subtrees.size();
  for (auto u : g[node])
    if (u - heavy[node]) dfs(u, 1);
  int secondMaxi = (split >= 2 ? subtrees[split - 2].first : -1);
  if (heavy[node] + 1) {
    dfs(heavy[node], 0);
    if (node == 0) {
    }
    if (sz[heavy[node]] >= n - sz[node]) {
      binary_search(child, secondMaxi - 1, sz[heavy[node]], subtrees[0].first,
                    node, 0);
    }
  }
  for (auto u : g[node])
    if (u - heavy[node]) Insert(u);
  ok(father, sz[node], -1);
  if (n - sz[node] > (heavy[node] + 1 ? sz[heavy[node]] : 0)) {
    binary_search(up, secondMaxi - 1, n - sz[node], subtrees[0].first, node, 0);
    binary_search(father, secondMaxi - 1, n - sz[node], subtrees[0].first, node,
                  sz[node]);
  }
  ok(child, sz[node], 1);
  if (cl) {
    Clear(node);
  }
}
int32_t main() {
  n = in();
  memset(heavy, -1, sizeof heavy);
  for (int i = 0; i < n; i++) {
    ans[i] = n - 1;
    int from = in() - 1, to = in() - 1;
    if (from + 1)
      g[from].push_back(to);
    else
      rt = to;
  }
  pre_calc(rt);
  dfs(rt, 0);
  for (int i = 0; i < n; i++) cout << ans[i] << " ";
  cout << "\n";
}
