#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int inf = 1e9;
int n;
int fa[N];
struct edge {
  int to, Next;
} ed[N];
int edcnt, head[N];
void addedge(int u, int v) {
  ed[++edcnt] = (edge){v, head[u]};
  head[u] = edcnt;
}
int sz[N], son[N], mn[N], mx2[N];
bool can[N];
void dfsson(int u) {
  sz[u] = 1;
  son[u] = 0;
  mn[u] = inf;
  for (int i = head[u]; i; i = ed[i].Next) {
    int v = ed[i].to;
    dfsson(v);
    sz[u] += sz[v];
    if (sz[v] > sz[son[u]]) {
      mx2[u] = sz[son[u]];
      son[u] = v;
      can[u] = true;
    } else if (sz[v] == sz[son[u]])
      can[u] = false;
    else if (sz[v] > mx2[u])
      mx2[u] = sz[v];
    if (sz[v] < mn[u]) mn[u] = sz[v];
  }
  if (fa[u]) {
    if (n - sz[u] > sz[son[u]]) {
      mx2[u] = sz[son[u]];
      son[u] = 0;
      can[u] = true;
    } else if (n - sz[u] == sz[son[u]])
      can[u] = false;
    else if (n - sz[u] > mx2[u])
      mx2[u] = n - sz[u];
    if (n - sz[u] < mn[u]) mn[u] = n - sz[u];
  }
  if (can[u]) {
    if ((!son[u] && n - sz[u] == mn[u]) || (son[u] && sz[son[u]] == mn[u]))
      can[u] = false;
  }
}
struct segtree {
  int ndcnt, root[N];
  int sum[N * 25], ls[N * 25], rs[N * 25];
  void change(int l, int r, int &x, int Pos, int Val) {
    if (!x) x = ++ndcnt;
    sum[x] += Val;
    if (l == r) return;
    int mid = (l + r) >> 1;
    if (Pos <= mid)
      change(l, mid, ls[x], Pos, Val);
    else
      change(mid + 1, r, rs[x], Pos, Val);
  }
  void change(int x, int Pos, int Val) { change(1, n, root[x], Pos, Val); }
  int querypre1(int l, int r, int x, int Pos) {
    if (!sum[x]) return n + 1;
    if (l == r) return l;
    int mid = (l + r) >> 1;
    if (Pos >= r) {
      if (sum[rs[x]])
        return querypre1(mid + 1, r, rs[x], Pos);
      else
        return querypre1(l, mid, ls[x], Pos);
    }
    if (Pos > mid) {
      int ret = querypre1(mid + 1, r, rs[x], Pos);
      if (ret != n + 1) return ret;
    }
    return querypre1(l, mid, ls[x], Pos);
  }
  int querypre1(int x, int Pos) {
    if (Pos < 1) return n + 1;
    return querypre1(1, n, root[x], Pos);
  }
  int querynxt1(int l, int r, int x, int Pos) {
    if (!sum[x]) return 0;
    if (l == r) return l;
    int mid = (l + r) >> 1;
    if (Pos <= l) {
      if (sum[ls[x]])
        return querynxt1(l, mid, ls[x], Pos);
      else
        return querynxt1(mid + 1, r, rs[x], Pos);
    }
    if (Pos <= mid) {
      int ret = querynxt1(l, mid, ls[x], Pos);
      if (ret) return ret;
    }
    return querynxt1(mid + 1, r, rs[x], Pos);
  }
  int querynxt1(int x, int Pos) {
    if (Pos > n) return 0;
    return querynxt1(1, n, root[x], Pos);
  }
  int querypre2(int l, int r, int x, int y, int z, int Pos) {
    if (!(sum[x] - sum[y] - sum[z])) return n + 1;
    if (l == r) return l;
    int mid = (l + r) >> 1;
    if (Pos >= r) {
      if (sum[rs[x]] - sum[rs[y]] - sum[rs[z]])
        return querypre2(mid + 1, r, rs[x], rs[y], rs[z], Pos);
      else
        return querypre2(l, mid, ls[x], ls[y], ls[z], Pos);
    }
    if (Pos > mid) {
      int ret = querypre2(mid + 1, r, rs[x], rs[y], rs[z], Pos);
      if (ret != n + 1) return ret;
    }
    return querypre2(l, mid, ls[x], ls[y], ls[z], Pos);
  }
  int querypre2(int x, int Pos) {
    if (Pos < 1) return n + 1;
    return querypre2(1, n, root[0], root[n + 1], root[x], Pos);
  }
  int querynxt2(int l, int r, int x, int y, int z, int Pos) {
    if (!(sum[x] - sum[y] - sum[z])) return 0;
    if (l == r) return l;
    int mid = (l + r) >> 1;
    if (Pos <= l) {
      if (sum[ls[x]] - sum[ls[y]] - sum[ls[z]])
        return querynxt2(l, mid, ls[x], ls[y], ls[z], Pos);
      else
        return querynxt2(mid + 1, r, rs[x], rs[y], rs[z], Pos);
    }
    if (Pos <= mid) {
      int ret = querynxt2(l, mid, ls[x], ls[y], ls[z], Pos);
      if (ret) return ret;
    }
    return querynxt2(mid + 1, r, rs[x], rs[y], rs[z], Pos);
  }
  int querynxt2(int x, int Pos) {
    if (Pos > n) return 0;
    return querynxt2(1, n, root[0], root[n + 1], root[x], Pos);
  }
  void merge(int l, int r, int &x, int y) {
    if (!x || !y) {
      x = x + y;
      return;
    }
    sum[x] = sum[x] + sum[y];
    if (l == r) return;
    int mid = (l + r) >> 1;
    merge(l, mid, ls[x], ls[y]);
    merge(mid + 1, r, rs[x], rs[y]);
  }
  void merge(int x, int y) { merge(1, n, root[x], root[y]); }
} Sg;
int ans[N];
void dfssolve(int u) {
  bool flag = false;
  if (fa[u]) Sg.change(n + 1, sz[fa[u]], 1);
  for (int i = head[u]; i; i = ed[i].Next) dfssolve(ed[i].to);
  if (can[u]) {
    if (son[u]) {
      ans[u] = sz[son[u]];
      int ret = Sg.querypre1(son[u], (sz[son[u]] - mn[u]) / 2);
      if (ret != n + 1)
        ans[u] = (ans[u] < sz[son[u]] - ret ? ans[u] : sz[son[u]] - ret);
      ret = Sg.querynxt1(son[u], (sz[son[u]] - mn[u] + 1) / 2);
      if (ret) ans[u] = (ans[u] < mn[u] + ret ? ans[u] : mn[u] + ret);
    } else {
      ans[u] = n - sz[u];
      int ret = Sg.querypre1(n + 1, (n - sz[u] - mn[u]) / 2 + sz[u]) - sz[u];
      if (ret != n + 1 - sz[u])
        ans[u] = (ans[u] < n - sz[u] - ret ? ans[u] : n - sz[u] - ret);
      ret = Sg.querynxt1(n + 1, (n - sz[u] - mn[u] + 1) / 2 + sz[u]) - sz[u];
      if (ret != -sz[u]) ans[u] = (ans[u] < mn[u] + ret ? ans[u] : mn[u] + ret);
      flag = true;
      Sg.change(u, sz[u], 1);
      for (int i = head[u]; i; i = ed[i].Next) Sg.merge(u, ed[i].to);
      ret = Sg.querypre2(u, (n - sz[u] - mn[u]) / 2);
      if (ret != n + 1)
        ans[u] = (ans[u] < n - sz[u] - ret ? ans[u] : n - sz[u] - ret);
      ret = Sg.querynxt2(u, (n - sz[u] - mn[u] + 1) / 2);
      if (ret) ans[u] = (ans[u] < mn[u] + ret ? ans[u] : mn[u] + ret);
    }
    ans[u] = (ans[u] > mx2[u] ? ans[u] : mx2[u]);
  } else {
    if (son[u])
      ans[u] = sz[son[u]];
    else
      ans[u] = n - sz[u];
  }
  if (fa[u]) Sg.change(n + 1, sz[fa[u]], -1);
  if (!flag) {
    Sg.change(u, sz[u], 1);
    for (int i = head[u]; i; i = ed[i].Next) Sg.merge(u, ed[i].to);
  }
}
int main() {
  scanf("%d", &n);
  int root;
  for (int i = 1; i <= n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    if (u) {
      fa[v] = u;
      addedge(u, v);
    } else
      root = v;
  }
  dfsson(root);
  for (int i = 1; i <= n; i++) Sg.change(0, sz[i], 1);
  dfssolve(root);
  for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
  return 0;
}
