#include <bits/stdc++.h>
using namespace std;
map<int, int> mp[4];
vector<int> v[100010];
int size[100010], son[100010], mn[100010], mx2[100010], ans[100010], n, m, rt;
inline int rd() {
  int x = 0;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    ;
  for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
  return x;
}
inline void add(int x, int y, int hh) {
  mp[hh][x] += y;
  mp[3][x] -= y;
  if (!mp[3][x]) mp[3].erase(x);
  if (!mp[hh][x]) mp[hh].erase(x);
}
inline void insert(int x, int y, int hh) {
  add(size[x], y, hh);
  for (int i = 0; i < v[x].size(); i++) insert(v[x][i], y, hh);
}
inline int find(int x, int y, int l, int r, int hh, int z) {
  if (x == y && x >= l && x <= r) return x;
  while (l <= r) {
    int mid = (l + r) >> 1;
    map<int, int>::iterator t = mp[hh].lower_bound(y - mid + z);
    if (t != mp[hh].end() && t->first <= mid - x + z)
      r = mid - 1;
    else
      l = mid + 1;
  }
  return l;
}
inline void work(int x) {
  if (size[x] == n)
    ans[x] = find(mn[x], size[son[x]], mx2[x], size[son[x]], 1, 0);
  else if (size[son[x]] > n - size[x])
    ans[x] =
        find((mn[x] < n - size[x] ? mn[x] : n - size[x]), size[son[x]],
             (n - size[x] > mx2[x] ? n - size[x] : mx2[x]), size[son[x]], 1, 0);
  else if (!son[x])
    ans[x] = n - size[x];
  else
    ans[x] =
        (find(mn[x], n - size[x], size[son[x]], n - size[x], 0, size[x]) <
                 find(mn[x], n - size[x], size[son[x]], n - size[x], 3, 0)
             ? find(mn[x], n - size[x], size[son[x]], n - size[x], 0, size[x])
             : find(mn[x], n - size[x], size[son[x]], n - size[x], 3, 0));
}
inline void pre(int x) {
  size[x] = 1;
  mn[x] = n;
  for (int i = 0; i < v[x].size(); i++) {
    pre(v[x][i]);
    size[x] += size[v[x][i]];
    mn[x] = (mn[x] < size[v[x][i]] ? mn[x] : size[v[x][i]]);
    if (size[v[x][i]] > size[son[x]])
      mx2[x] = size[son[x]], son[x] = v[x][i];
    else if (size[v[x][i]] > mx2[x])
      mx2[x] = size[v[x][i]];
  }
  mp[3][size[x]]++;
  if (size[x] < n) mn[x] = (mn[x] < n - size[x] ? mn[x] : n - size[x]);
}
inline void dfs(int x, bool bo) {
  add(size[x], 1, 0);
  for (int i = 0; i < v[x].size(); i++)
    if (v[x][i] != son[x]) dfs(v[x][i], 0);
  if (son[x]) dfs(son[x], 1);
  for (int i = 0; i < v[x].size(); i++)
    if (v[x][i] != son[x]) insert(v[x][i], 1, 2);
  if (!(--mp[0][size[x]])) mp[0].erase(size[x]);
  work(x);
  mp[3][size[x]]++;
  if (bo) {
    add(size[x], 1, 1);
    for (map<int, int>::iterator t = mp[2].begin(); t != mp[2].end();) {
      mp[1][t->first] += t->second;
      mp[2].erase(t++);
    }
  } else
    for (int i = 0; i < v[x].size(); i++) {
      int t = v[x][i];
      if (t != son[x])
        insert(t, -1, 2);
      else
        insert(t, -1, 1);
    }
}
int main() {
  n = rd();
  if (n == 1) {
    puts("0");
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    int x = rd(), y = rd();
    if (x)
      v[x].push_back(y);
    else
      rt = y;
  }
  pre(rt);
  dfs(rt, 1);
  for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
  return 0;
}
