#include <bits/stdc++.h>
using namespace std;
long long sz[int(3e5 + 500)];
vector<long long> v[int(3e5 + 500)];
multiset<long long> cs;
multiset<long long> cp;
multiset<long long> *st[int(3e5 + 500)];
long long pas[int(3e5 + 500)];
long long N;
void qck(long long x) {
  sz[x] = 1;
  for (auto y : v[x]) {
    qck(y);
    sz[x] += sz[y];
  }
}
void go(long long x, long long kp) {
  long long mx = -1;
  for (auto y : v[x])
    if (mx == -1 || sz[y] > sz[mx]) mx = y;
  cs.erase(cs.find(sz[x]));
  cp.insert(sz[x]);
  for (auto y : v[x]) {
    if (y == mx) continue;
    go(y, 0);
  }
  if (mx == -1) {
    st[x] = new multiset<long long>();
    st[x]->insert(sz[x]);
    cp.erase(cp.find(sz[x]));
    pas[x] = cs.size() + cp.size();
    if (!kp) cs.insert(sz[x]);
    return;
  }
  go(mx, 1);
  cp.erase(cp.find(sz[x]));
  for (auto y : v[x]) {
    if (y == mx) continue;
    for (auto p : *st[y]) cs.erase(cs.find(p));
  }
  if (cp.size() + cs.size() == 0 && v[x].size() == 1) {
    pas[x] = st[v[x][0]]->size();
    return;
  }
  long long l = 0, r = N, q = 1;
  while (l < r) {
    long long m = (l + r) >> 1;
    long long c = -1, sm = -1;
    for (auto y : v[x]) {
      if (sz[y] > m) {
        if (c == -1)
          c = y;
        else
          c = -2;
      }
      if (sm == -1 || sz[y] < sz[sm]) sm = y;
    }
    if (cp.size() + cs.size() > m) {
      if (c == -1)
        c = -3;
      else
        c = -2;
    }
    if (sm == -1 || (cp.size() + cs.size() && cp.size() + cs.size() < sz[sm]))
      sm = -3;
    if (c == -2) {
      l = m + 1;
      continue;
    } else if (c == -1) {
      r = m;
      continue;
    }
    long long smz;
    if (sm != -3)
      smz = sz[sm];
    else
      smz = cp.size() + cs.size();
    if (c != -3) {
      auto it = st[c]->lower_bound(sz[c] - m);
      if (it == st[c]->end()) {
        l = m + 1;
        continue;
      }
      if (smz + (*it) > m) {
        l = m + 1;
        continue;
      } else {
        r = m;
        continue;
      }
    } else {
      auto it1 = cp.lower_bound(cp.size() + cs.size() - m + sz[x]);
      auto it2 = cs.lower_bound(cp.size() + cs.size() - m);
      if (it1 == cp.end() && it2 == cs.end()) {
        l = m + 1;
        continue;
      }
      long long k = ((long long)(1e9 + 1));
      if (it1 != cp.end()) k = min(k, *it1 - sz[x]);
      if (it2 != cs.end()) k = min(k, *it2);
      if (smz + k > m) {
        l = m + 1;
        continue;
      } else {
        r = m;
        continue;
      }
    }
  }
  pas[x] = l;
  if (!kp) {
    cs.insert(sz[x]);
    for (auto y : v[x]) {
      for (auto p : *st[y]) cs.insert(p);
    }
  }
  st[x] = st[mx];
  st[x]->insert(sz[x]);
  for (auto y : v[x]) {
    if (y == mx) continue;
    for (auto p : *st[y]) st[x]->insert(p);
  }
  return;
}
int main() {
  long long n, k, p, rt;
  cin >> n;
  N = n;
  for (int i = 0; i < n; i++) {
    cin >> k >> p;
    if (k == 0)
      rt = p;
    else
      v[k].push_back(p);
  }
  qck(rt);
  for (int i = 1; i <= n; i++) cs.insert(sz[i]);
  go(rt, 1);
  for (int i = 1; i <= n; i++) cout << pas[i] << endl;
  return 0;
}
