#include <bits/stdc++.h>
using namespace std;
inline int read() {
  char ch = getchar();
  int nega = 1;
  while (!isdigit(ch)) {
    if (ch == '-') nega = -1;
    ch = getchar();
  }
  int ans = 0;
  while (isdigit(ch)) {
    ans = ans * 10 + ch - 48;
    ch = getchar();
  }
  if (nega == -1) return -ans;
  return ans;
}
void print(vector<int> x) {
  for (int i = 0; i < (int)x.size(); i++)
    printf("%d%c", x[i], " \n"[i == (int)x.size() - 1]);
}
inline int min(int x, int y, int z) { return min(x, min(y, z)); }
inline int max(int x, int y, int z) { return max(x, max(y, z)); }
vector<int> G[100005], v[100005];
int fa[100005], siz[100005], n, rt, son[100005];
int minn[100005], maxn[100005], id[100005], ans[100005];
multiset<int> s[100005];
void dfs1(int u) {
  siz[u] = 1;
  for (int V : G[u]) {
    dfs1(V), siz[u] += siz[V];
    v[u].push_back(siz[V]);
    if (siz[V] > maxn[u]) maxn[u] = siz[V], id[u] = V;
    minn[u] = min(minn[u], siz[V]);
    if (siz[V] > siz[son[u]]) son[u] = V;
  }
  if (u == rt) return;
  v[u].push_back(n - siz[u]);
  if (n - siz[u] > maxn[u]) maxn[u] = n - siz[u], id[u] = -1;
  minn[u] = min(minn[u], n - siz[u]);
}
int getans(const vector<int> &v, const multiset<int> &s) {
  int A = (v[0] - v.back()) / 2;
  auto x = s.lower_bound(A);
  int ans = v[0];
  if (x != s.end()) ans = min(ans, max(v[0] - (*x), v[1], v.back() + (*x)));
  if (x == s.begin()) return ans;
  --x;
  if (x != s.end()) ans = min(ans, max(v[0] - (*x), v[1], v.back() + (*x)));
  return ans;
}
void merge(multiset<int> &u, multiset<int> &v) {
  if (u.size() < v.size()) swap(u, v);
  for (auto i : v) u.insert(i);
}
void dfs2(int u) {
  for (int v : G[u]) dfs2(v);
  if (id[u] != -1 && v[u].size() != 1) ans[u] = getans(v[u], s[id[u]]);
  s[u].insert(siz[u]);
  for (int v : G[u]) merge(s[u], s[v]);
}
multiset<int> cur, cur2;
int getans2(int u, const vector<int> &v) {
  int A = (v[0] - v.back()) / 2;
  auto x = cur2.lower_bound(A + siz[u]);
  int ans = v[0];
  if (x != cur2.end())
    ans =
        min(ans, max(v[0] - ((*x) - siz[u]), v[1], v.back() + ((*x) - siz[u])));
  if (x == cur2.begin()) return ans;
  --x;
  if (x != cur2.end())
    ans =
        min(ans, max(v[0] - ((*x) - siz[u]), v[1], v.back() + ((*x) - siz[u])));
  return ans;
}
void ins(int u) {
  cur.erase(cur.find(siz[u]));
  for (int v : G[u]) ins(v);
}
void del(int u) {
  cur.insert(siz[u]);
  for (int v : G[u]) del(v);
}
void dfs3(int u, int h) {
  if (!son[u]) return;
  cur.erase(cur.find(siz[u]));
  cur2.insert(siz[u]);
  for (int v : G[u]) {
    if (v == son[u]) continue;
    dfs3(v, 0);
  }
  dfs3(son[u], 1);
  cur2.erase(cur2.find(siz[u]));
  for (int v : G[u])
    if (v != son[u]) ins(v);
  if (id[u] == -1 && v[u].size() != 1)
    ans[u] = min(getans(v[u], cur), getans2(u, v[u]));
  if (!h) {
    for (int v : G[u]) del(v);
    cur.insert(siz[u]);
  }
}
signed main() {
  cin >> n;
  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    int u = read(), v = read();
    if (u == 0)
      rt = v;
    else
      G[u].push_back(v), fa[v] = u;
  }
  for (int i = 1; i <= n; i++) minn[i] = 0x3f3f3f3f;
  dfs1(rt);
  for (int i = 1; i <= n; i++) sort(v[i].begin(), v[i].end(), greater<int>());
  for (int i = 1; i <= n; i++)
    if (v[i].size() == 1) ans[i] = v[i][0];
  dfs2(rt);
  for (int i = 1; i <= n; i++) cur.insert(siz[i]);
  dfs3(rt, 1);
  for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
  return 0;
}
