#include <bits/stdc++.h>
const int N = 1e5 + 5;
int n, rt;
struct Edge {
  int to, next;
} e[N << 1];
int head[N], cnt = 0;
void Add(int x, int y) {
  e[++cnt].next = head[x];
  e[cnt].to = y;
  head[x] = cnt;
}
std ::pair<int, int> mx[N];
int mn[N];
void Check(int x, int y) {
  if (y > mx[x].first)
    mx[x].second = mx[x].first, mx[x].first = y;
  else
    mx[x].second = std ::max(mx[x].second, y);
  mn[x] = std ::min(mn[x], y);
}
int son[N], siz[N], ans[N];
std ::map<int, int> oth, path, in;
void Dfs1(int u, int fa) {
  siz[u] = 1;
  mn[u] = 1e9;
  for (int i = head[u]; i; i = e[i].next) {
    int v = e[i].to;
    if (v == fa) continue;
    Dfs1(v, u);
    siz[u] += siz[v];
    if (siz[v] > siz[son[u]]) son[u] = v;
    Check(u, siz[v]);
  }
  if (n != siz[u]) Check(u, n - siz[u]);
  oth[siz[u]]++;
}
void Inv(int x, int y) {
  oth[x] += y;
  if (!oth[x]) oth.erase(x);
}
void InOth(int u, int fa) {
  Inv(siz[u], -1);
  for (int i = head[u]; i; i = e[i].next) {
    int v = e[i].to;
    if (v == fa) continue;
    InOth(v, u);
  }
}
void InIn(int u, int fa) {
  in[siz[u]]++;
  for (int i = head[u]; i; i = e[i].next) {
    int v = e[i].to;
    if (v == fa) continue;
    InIn(v, u);
  }
}
void Clear(int u, int fa) {
  Inv(siz[u], 1);
  for (int i = head[u]; i; i = e[i].next) {
    int v = e[i].to;
    if (v == fa) continue;
    Clear(v, u);
  }
}
void Dsu(int u, int fa) {
  path[siz[u]]++;
  for (int i = head[u]; i; i = e[i].next) {
    int v = e[i].to;
    if (v == fa || v == son[u]) continue;
    Inv(siz[v], -1);
    Dsu(v, u);
    Inv(siz[v], 1);
    Clear(v, u);
    in.clear();
  }
  if (son[u]) {
    Inv(siz[son[u]], -1);
    Dsu(son[u], u);
    Inv(siz[son[u]], 1);
  }
  for (int i = head[u]; i; i = e[i].next) {
    int v = e[i].to;
    if (v == fa || v == son[u]) continue;
    InOth(v, u);
  }
  in[0] = 1;
  int l = mx[u].second, r = n;
  if (n - siz[u] < siz[son[u]]) {
    while (l < r) {
      int mid = (l + r) >> 1;
      auto it = in.lower_bound(mx[u].first - mid);
      if (it != in.end() && it->first <= mid - mn[u])
        r = mid;
      else
        l = mid + 1;
    }
  } else {
    while (l < r) {
      int mid = (l + r) >> 1, flag = 0;
      auto it = oth.lower_bound(mx[u].first - mid);
      if (it != oth.end() && it->first <= mid - mn[u]) flag = 1;
      it = path.lower_bound(mx[u].first + siz[u] - mid);
      if (it != path.end() && it->first - siz[u] <= mid - mn[u]) flag = 1;
      if (flag)
        r = mid;
      else
        l = mid + 1;
    }
  }
  ans[u] = l;
  for (int i = head[u]; i; i = e[i].next) {
    int v = e[i].to;
    if (v == fa || v == son[u]) continue;
    InIn(v, u);
  }
  in[siz[u]]++;
  Inv(siz[u], -1);
  path[siz[u]]--;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    if (!u || !v)
      rt = u + v;
    else
      Add(u, v), Add(v, u);
  }
  if (n == 1) {
    puts("0");
    return 0;
  }
  Dfs1(rt, 0);
  Dsu(rt, 0);
  for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
  return 0;
}
