#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
inline int read() {
  int x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') ch = getchar();
  while ('0' <= ch && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return x;
}
int n, head[N], rt, sz[N], son[N], a[N], b[N], c[N], ans[N], cnt;
multiset<int> s1[N], s2, s3;
struct edge {
  int to, nxt;
} e[N];
void add(int a, int b) {
  e[++cnt].to = b;
  e[cnt].nxt = head[a];
  head[a] = cnt;
}
void dfs(int u, int f) {
  sz[u] = 1, a[u] = 0, b[u] = 0, c[u] = n;
  for (register int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (v == f) continue;
    dfs(v, u);
    if (sz[v] > sz[son[u]]) son[u] = v;
    c[u] = min(c[u], sz[v]), b[u] = max(b[u], sz[v]);
    if (b[u] > a[u]) swap(b[u], a[u]);
    sz[u] += sz[v];
  }
  b[u] = max(b[u], n - sz[u]);
  if (b[u] > a[u]) swap(b[u], a[u]);
  if (f) c[u] = min(c[u], n - sz[u]);
  if (!b[u]) b[u] = a[u];
  if (f) s3.insert(sz[u]);
}
bool check(int tp, int mid, int u, int l, int r) {
  multiset<int>::iterator it;
  if (tp) {
    it = s1[son[u]].lower_bound(r - mid);
    if (it != s1[son[u]].end() && (*it) <= mid - l) return true;
  } else {
    it = s3.lower_bound(r - mid);
    if (it != s3.end() && (*it) <= mid - l) return true;
    it = s2.lower_bound(r - mid + sz[u]);
    if (it != s2.end() && (*it) <= mid - l + sz[u]) return true;
  }
  return false;
}
void get(int u, int f) {
  if (f) {
    s3.erase(s3.find(sz[u]));
    if (f != rt) s2.insert(sz[f]);
  }
  for (register int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (v == son[u] || v == f) continue;
    get(v, u);
    for (multiset<int>::iterator it = s1[v].begin(); it != s1[v].end(); it++)
      s3.insert(*it);
  }
  if (son[u]) get(son[u], u);
  for (register int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (v == son[u] || v == f) continue;
    for (multiset<int>::iterator it = s1[v].begin(); it != s1[v].end(); it++)
      s3.erase(s3.find(*it));
  }
  ans[u] = a[u];
  int l = b[u], r = a[u], tag = (a[u] == sz[son[u]]);
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(tag, mid, u, c[u], a[u]))
      ans[u] = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  if (son[u]) swap(s1[son[u]], s1[u]);
  for (register int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (v == son[u] || v == f) continue;
    for (multiset<int>::iterator it = s1[v].begin(); it != s1[v].end(); it++)
      s1[u].insert(*it);
  }
  if (f && f != rt) s2.erase(s2.find(sz[f]));
  s1[u].insert(sz[u]);
}
int main() {
  n = read();
  for (register int i = 1; i <= n; i++) {
    int f = read(), u = read();
    if (!f)
      rt = u;
    else
      add(f, u);
  }
  dfs(rt, 0), get(rt, 0);
  for (register int i = 1; i <= n; i++) printf("%d\n", ans[i]);
}
