#include <bits/stdc++.h>
using namespace std;
int n, u, f, root, rt_all, rt_path, rt[100005], fa[100005], ans[100005];
int read() {
  int x = 0;
  char ch = getchar();
  while (!isdigit(ch)) ch = getchar();
  while (isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
  return x;
}
namespace SBT {
double alpha = 0.55;
int lc[400005], rc[400005], tot;
int val[400005], c[400005], siz[400005], rsiz[400005];
int dl[400005], tp, q[400005], qc[400005], qn;
int newnode(int v, int ct) {
  int u = tp ? dl[tp--] : ++tot;
  lc[u] = rc[u] = 0, val[u] = v, c[u] = siz[u] = ct, rsiz[u] = 1;
  return u;
}
void pushup(int u) {
  rsiz[u] = rsiz[lc[u]] + rsiz[rc[u]] + 1;
  siz[u] = siz[lc[u]] + siz[rc[u]] + c[u];
}
void Zig(int &u) {
  int v = lc[u];
  lc[u] = rc[v], rc[v] = u;
  pushup(u), pushup(v);
  u = v;
}
void Zag(int &u) {
  int v = rc[u];
  rc[u] = lc[v], lc[v] = u;
  pushup(u), pushup(v);
  u = v;
}
void ins(int &u, int v, int ct = 1) {
  if (!u) {
    u = newnode(v, ct);
    return;
  }
  siz[u] += ct;
  if (val[u] == v)
    c[u] += ct;
  else if (v < val[u]) {
    ins(lc[u], v, ct);
    if (rsiz[lc[u]] > rsiz[u] * alpha) Zig(u);
  } else {
    ins(rc[u], v, ct);
    if (rsiz[rc[u]] > rsiz[u] * alpha) Zag(u);
  }
  pushup(u);
}
void del(int &u, int v) {
  if (!u) return;
  if (val[u] == v) {
    if (c[u] > 1)
      --c[u], --siz[u];
    else if (!lc[u] || !rc[u])
      dl[++tp] = u, u = lc[u] | rc[u];
    else if (rsiz[lc[u]] < rsiz[rc[u]])
      Zag(u), del(u, v);
    else
      Zig(u), del(u, v);
    if (u) pushup(u);
    return;
  }
  if (v < val[u])
    del(lc[u], v);
  else
    del(rc[u], v);
  pushup(u);
}
int Rank(int u, int v, int ans = 0) {
  while (u) {
    if (val[u] == v) return ans + siz[lc[u]] + c[u];
    if (val[u] < v)
      ans += siz[lc[u]] + c[u], u = rc[u];
    else
      u = lc[u];
  }
  return ans;
}
int range(int rt, int l, int r) { return Rank(rt, r) - Rank(rt, l - 1); }
void inorder_dfs(int u) {
  if (lc[u]) inorder_dfs(lc[u]);
  q[++qn] = val[u], qc[qn] = c[u], dl[++tp] = u;
  if (rc[u]) inorder_dfs(rc[u]);
}
void Join(int &u, int &v) {
  if (rsiz[u] < rsiz[v]) std::swap(u, v);
  qn = 0, inorder_dfs(v), v = 0;
  for (int i = 1; i <= qn; ++i) ins(u, q[i], qc[i]);
}
}  // namespace SBT
using SBT::del;
using SBT::ins;
using SBT::Join;
using SBT::range;
namespace GRAPH {
int Head[100005], Next[100005 << 1], V[100005 << 1], siz[100005], cnt = 0;
void add(int u, int v) { Next[++cnt] = Head[u], V[cnt] = v, Head[u] = cnt; }
void pre_dfs(int u) {
  siz[u] = 1;
  for (int i = Head[u], v = V[i]; i; v = V[i = Next[i]])
    if (v != fa[u]) pre_dfs(v), siz[u] += siz[v];
}
bool check(int u, int mxson, int mn, int mx, int lim) {
  int l = mx - lim, r = lim - mn;
  if (l > r) return false;
  if (mxson == fa[u])
    return (range(rt_all, l, r) - range(rt[u], l, r) +
            range(rt_path, l + siz[u], r + siz[u])) > 0;
  else
    return range(rt[mxson], l, r) > 0;
}
void solve(int u) {
  del(rt_all, siz[u]);
  if (u != root) ins(rt_path, siz[u]);
  int mn = n + 1, mx = 0, submx = 0, mxson = 0;
  for (int i = Head[u], v = V[i]; i; v = V[i = Next[i]]) {
    int siz_v = v == fa[u] ? n - siz[u] : siz[v];
    if (v != fa[u]) solve(v);
    mn = min(mn, siz_v);
    if (siz_v > mx)
      submx = mx, mxson = v, mx = siz_v;
    else if (siz_v > submx)
      submx = siz_v;
  }
  ins(rt_all, siz[u]), ins(rt[u], siz[u]);
  if (u != root) del(rt_path, siz[u]);
  if (mxson == fa[u])
    for (int i = Head[u], v = V[i]; i; v = V[i = Next[i]])
      if (v != fa[u]) Join(rt[u], rt[v]);
  int l = submx == 0 ? mx : submx, r = mx, mid;
  while (l < r)
    check(u, mxson, mn, mx, mid = l + r >> 1) ? r = mid : l = mid + 1;
  ans[u] = l;
  if (mxson != fa[u])
    for (int i = Head[u], v = V[i]; i; v = V[i = Next[i]])
      if (v != fa[u]) Join(rt[u], rt[v]);
}
}  // namespace GRAPH
using GRAPH::add;
int main() {
  n = read();
  for (int i = 1; i <= n; ++i) {
    f = read(), u = read();
    if (!f)
      root = u;
    else
      add(fa[u] = f, u), add(u, f);
  }
  GRAPH::pre_dfs(root);
  for (int i = 1; i <= n; ++i) ins(rt_all, GRAPH::siz[i]);
  GRAPH::solve(root);
  for (int i = 1; i <= n; ++i) printf("%d\n", ans[i]);
}
