#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
struct Edge {
  int to, next;
} edge[N];
int head[N], num;
void add_edge(int a, int b) { edge[++num] = (Edge){b, head[a]}, head[a] = num; }
int n, rt, mx[N], idx[N], mn[N], idn[N], tid[N], cnt, sz[N];
int f[N], dep[N];
void dfs1(int x, int fa) {
  tid[x] = ++cnt, f[x] = fa, dep[x] = dep[fa] + 1, sz[x] = 1, mn[x] = 1e9;
  for (int i = head[x]; i; i = edge[i].next)
    if (edge[i].to != fa) {
      dfs1(edge[i].to, x);
      sz[x] += sz[edge[i].to];
      if (sz[edge[i].to] > mx[x]) mx[x] = sz[edge[i].to], idx[x] = edge[i].to;
      if (sz[edge[i].to] < mn[x]) mn[x] = sz[edge[i].to], idn[x] = edge[i].to;
    }
  if (x != rt) {
    if (n - sz[x] > mx[x]) mx[x] = n - sz[x], idx[x] = f[x];
    if (n - sz[x] < mn[x]) mn[x] = n - sz[x], idn[x] = f[x];
  }
}
multiset<int> t[N * 4], t0;
void modify(int x, int l, int r, int a, int b, int ty) {
  if (ty == 1)
    t[x].insert(b);
  else {
    t[x].erase(t[x].lower_bound(b));
  }
  if (l == r) return;
  int mid = (l + r) >> 1;
  if (a <= mid)
    modify(x << 1, l, mid, a, b, ty);
  else
    modify(x << 1 | 1, mid + 1, r, a, b, ty);
}
pair<int, int> get(multiset<int> &tx, int c) {
  if (!tx.size()) return make_pair(1e9, -1e9);
  multiset<int>::iterator t1 = tx.lower_bound(c);
  if (t1 == tx.end()) {
    t1--;
    return make_pair(1e9, *t1);
  } else if (t1 == tx.begin())
    return make_pair(*t1, -1e9);
  multiset<int>::iterator t2 = t1;
  t2--;
  return make_pair(*t1, *t2);
}
pair<int, int> query(int x, int l, int r, int a, int b, int c) {
  if (a <= l && r <= b) return get(t[x], c);
  int mid = (l + r) >> 1, t1 = 1e9, t2 = -1e9;
  if (a <= mid) {
    pair<int, int> tmp = query(x << 1, l, mid, a, b, c);
    t1 = min(t1, tmp.first), t2 = max(t2, tmp.second);
  }
  if (b > mid) {
    pair<int, int> tmp = query(x << 1 | 1, mid + 1, r, a, b, c);
    t1 = min(t1, tmp.first), t2 = max(t2, tmp.second);
  }
  return make_pair(t1, t2);
}
int ans[N], d[N], tf[N];
pair<int, int> query0(int x, int l, int r, int a, int b, int c) {
  int ret1 = 1e9, ret2 = -1e9;
  for (int i = a; i <= b; i++) {
    if (tf[i] < c)
      ret2 = max(ret2, tf[i]);
    else
      ret1 = min(ret1, tf[i]);
  }
  return make_pair(ret1, ret2);
}
void calc(int x) {
  if (d[x] == 1 || n == 1)
    ans[x] = n - 1;
  else {
    int t1, t2;
    int s1 = idx[x], s2 = idn[x];
    ans[x] = 1e9;
    if (s1 == f[x]) {
      pair<int, int> tmp1 = query(1, 1, n, 1, tid[x] - 1, (mx[x] - mn[x]) / 2);
      pair<int, int> tmp2 =
          query(1, 1, n, tid[x] + sz[x], n, (mx[x] - mn[x]) / 2);
      pair<int, int> tmp0 = get(t0, (mx[x] - mn[x]) / 2 + sz[x]);
      t1 = min(tmp1.first, tmp2.first), t2 = max(tmp1.second, tmp2.second);
      t1 = min(t1, tmp0.first - sz[x]), t2 = max(t2, tmp0.second - sz[x]);
    } else {
      pair<int, int> tmp =
          query(1, 1, n, tid[s1], tid[s1] + sz[s1] - 1, (mx[x] - mn[x]) / 2);
      t1 = tmp.first, t2 = tmp.second;
    }
    int tmp1 = 0, tmp2 = 0;
    if (t1 != 1e9) {
      for (int i = head[x]; i; i = edge[i].next) {
        if (edge[i].to == s1)
          tmp1 = max(tmp1, sz[edge[i].to] - t1);
        else if (edge[i].to == s2)
          tmp1 = max(tmp1, sz[edge[i].to] + t1);
        else
          tmp1 = max(tmp1, sz[edge[i].to]);
      }
      if (f[x] == s1)
        tmp1 = max(tmp1, n - sz[x] - t1);
      else if (f[x] == s2)
        tmp1 = max(tmp1, n - sz[x] + t1);
      else
        tmp1 = max(tmp1, n - sz[x]);
    } else
      tmp1 = 1e9;
    if (t2 != -1e9) {
      for (int i = head[x]; i; i = edge[i].next) {
        if (edge[i].to == s1) {
          tmp2 = max(tmp2, sz[edge[i].to] - t2);
        } else if (edge[i].to == s2) {
          tmp2 = max(tmp2, sz[edge[i].to] + t2);
        } else
          tmp2 = max(tmp2, sz[edge[i].to]);
      }
      if (f[x] == s1)
        tmp2 = max(tmp2, n - sz[x] - t2);
      else if (f[x] == s2)
        tmp2 = max(tmp2, n - sz[x] + t2);
      else
        tmp2 = max(tmp2, n - sz[x]);
    } else
      tmp2 = 1e9;
    ans[x] = min(min(tmp1, tmp2), ans[x]);
  }
  modify(1, 1, n, tid[x], sz[x], -1);
  t0.insert(sz[x]);
  for (int i = head[x]; i; i = edge[i].next) calc(edge[i].to);
  modify(1, 1, n, tid[x], sz[x], 1);
  t0.erase(t0.lower_bound(sz[x]));
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (x) {
      add_edge(x, y);
      d[x]++, d[y]++;
    } else
      rt = y;
  }
  dfs1(rt, 0);
  for (int i = 1; i <= n; i++) {
    tf[tid[i]] = sz[i];
    modify(1, 1, n, tid[i], sz[i], 1);
  }
  calc(rt);
  for (int i = 1; i <= n; i++) {
    printf("%d\n", ans[i]);
  }
  return 0;
}
