#include <bits/stdc++.h>
using namespace std;
void File() {
  freopen("g.in", "r", stdin);
  freopen("g.out", "w", stdout);
}
template <typename T>
void read(T &_) {
  _ = 0;
  T f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) _ = (_ << 1) + (_ << 3) + (c ^ '0');
  _ *= f;
}
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
int n, rt, fa[maxn], ans[maxn];
int beg[maxn], to[maxn << 1], las[maxn << 1], cnte = 1;
int sz[maxn], in[maxn], dfn[maxn], cnt_dfn;
void add(int u, int v) { las[++cnte] = beg[u], beg[u] = cnte, to[cnte] = v; }
struct node {
  int lc, rc, sum;
} a[maxn << 6];
int root[maxn][2], cnt;
void insert(int &o, int l, int r, int p) {
  int now = ++cnt;
  a[now] = a[o], o = now, ++a[o].sum;
  if (l == r)
    return;
  else {
    if (p <= ((l + r) >> 1))
      insert(a[o].lc, l, ((l + r) >> 1), p);
    else
      insert(a[o].rc, ((l + r) >> 1) + 1, r, p);
  }
}
void dfs1(int u) {
  sz[u] = 1;
  ++cnt_dfn;
  dfn[cnt_dfn] = u;
  in[u] = cnt_dfn;
  for (int i = beg[u]; i; i = las[i]) {
    int v = to[i];
    dfs1(v);
    sz[u] += sz[v];
  }
}
void dfs2(int u) {
  if (u != rt) {
    root[u][0] = root[dfn[in[u] - 1]][0];
    insert(root[u][0], 1, n, sz[u]);
    root[u][1] = root[fa[u]][1];
    insert(root[u][1], 1, n, sz[u]);
  }
  for (int i = beg[u]; i; i = las[i]) {
    int v = to[i];
    dfs2(v);
  }
}
void init() {
  read(n);
  int fh, u;
  for (int i = 1, i_end_ = n; i <= i_end_; ++i) {
    read(fh), read(u);
    if (!fh)
      rt = u;
    else
      add(fa[u] = fh, u);
  }
  dfs1(rt);
  dfs2(rt);
}
int query_pre(int o1, int o2, int l, int r, int p) {
  if (a[o1].sum == a[o2].sum) return -inf;
  if (l == r) return l;
  if (p <= ((l + r) >> 1))
    return query_pre(a[o1].lc, a[o2].lc, l, ((l + r) >> 1), p);
  else {
    int res = query_pre(a[o1].rc, a[o2].rc, ((l + r) >> 1) + 1, r, p);
    if (res > -inf)
      return res;
    else
      return query_pre(a[o1].lc, a[o2].lc, l, ((l + r) >> 1), p);
  }
}
int query_nex(int o1, int o2, int l, int r, int p) {
  if (a[o1].sum == a[o2].sum) return inf;
  if (l == r) return l;
  if (p <= ((l + r) >> 1)) {
    int res = query_nex(a[o1].lc, a[o2].lc, l, ((l + r) >> 1), p);
    if (res < inf)
      return res;
    else
      return query_nex(a[o1].rc, a[o2].rc, ((l + r) >> 1) + 1, r, p);
  } else
    return query_nex(a[o1].rc, a[o2].rc, ((l + r) >> 1) + 1, r, p);
}
pair<int, int> qu[maxn];
void work() {
  for (int i = 1, i_end_ = n; i <= i_end_; ++i) {
    int c = 0;
    if (i != rt) qu[++c] = make_pair(sz[rt] - sz[i], fa[i]);
    for (int j = beg[i]; j; j = las[j]) qu[++c] = make_pair(sz[to[j]], to[j]);
    sort(qu + 1, qu + c + 1);
    if (!c)
      ans[i] = 0;
    else if (c == 1)
      ans[i] = qu[1].first;
    else {
      ans[i] = qu[c].first;
      int w = qu[c].first - (qu[c].first + qu[1].first) / 2;
      int u = qu[c].second, o1, o2, val;
      int pre = -inf, nex = inf;
      if (u != fa[i]) {
        o1 = root[u][0], o2 = root[dfn[in[u] + sz[u] - 1]][0];
        pre = max(pre, query_pre(o1, o2, 1, n, w));
        nex = min(nex, query_nex(o1, o2, 1, n, w));
      } else {
        o1 = root[i][1], o2 = root[i][0];
        pre = max(pre, query_pre(o1, o2, 1, n, w));
        nex = min(nex, query_nex(o1, o2, 1, n, w));
        o1 = 0, o2 = root[u][1];
        pre = max(pre, query_pre(o1, o2, 1, n, w + sz[i]) - sz[i]);
        nex = min(nex, query_nex(o1, o2, 1, n, w + sz[i]) - sz[i]);
        o1 = root[dfn[in[i] + sz[i] - 1]][0], o2 = root[dfn[n]][0];
        pre = max(pre, query_pre(o1, o2, 1, n, w));
        nex = min(nex, query_nex(o1, o2, 1, n, w));
      }
      val = max(max(qu[1].first + pre, qu[c].first - pre),
                c > 2 ? qu[c - 1].first : 0);
      ans[i] = min(ans[i], val);
      val = max(max(qu[1].first + nex, qu[c].first - nex),
                c > 2 ? qu[c - 1].first : 0);
      ans[i] = min(ans[i], val);
    }
  }
  for (int i = 1, i_end_ = n; i <= i_end_; ++i) printf("%d\n", ans[i]);
}
int main() {
  init();
  work();
  return 0;
}
