#include <bits/stdc++.h>
using namespace std;
int Root, N;
vector<int> G[100005];
int DP[100005];
map<int, int> S[3];
int Res[100005];
void Read() {
  cin >> N;
  for (int i = 1; i <= N; i++) {
    int parent, node;
    cin >> parent >> node;
    if (parent == 0)
      Root = node;
    else
      G[parent].push_back(node);
  }
}
void precalcSize(int node) {
  DP[node] = 1;
  for (int i = 0; i < G[node].size(); i++) {
    int neighb = G[node][i];
    precalcSize(neighb);
    DP[node] += DP[neighb];
  }
}
int findS(int node, bool up) {
  int Max = 0, Max2 = 0, Min = 0x3f3f3f3f;
  for (int i = 0; i < G[node].size(); i++) {
    int neighb = G[node][i];
    if (Max < DP[neighb]) {
      Max2 = Max;
      Max = DP[neighb];
    } else if (Max2 < DP[neighb])
      Max2 = DP[neighb];
    if (Min > DP[neighb]) Min = DP[neighb];
  }
  bool u = 0;
  if (Max < N - DP[node]) {
    u = 1;
    Max2 = Max;
    Max = N - DP[node];
  } else if (Max2 < N - DP[node])
    Max2 = N - DP[node];
  if (Min > N - DP[node] && N - DP[node] > 0) Min = N - DP[node];
  int left = Max2, right = Max, mid, sol = right;
  if (u != up) return 0;
  if (Max2 == 0) {
    return Max;
  }
  while (left <= right) {
    mid = (left + right) / 2;
    if (up == 0) {
      int val = Max - mid;
      int val2 = mid - Min;
      auto it = S[0].lower_bound(val);
      int x;
      if (it == S[0].end())
        x = 0x3f3f3f3f;
      else
        x = it->first;
      if (x <= val2) {
        sol = mid;
        right = mid - 1;
      } else
        left = mid + 1;
    } else {
      int val = Max - mid;
      int val2 = mid - Min;
      auto it = S[1].lower_bound(val + DP[node]);
      int x;
      if (it == S[1].end())
        x = 0x3f3f3f3f;
      else
        x = it->first;
      if (x <= val2 + DP[node]) {
        sol = mid;
        right = mid - 1;
        continue;
      }
      it = S[2].lower_bound(val);
      if (it == S[2].end())
        x = 0x3f3f3f3f;
      else
        x = it->first;
      if (x <= val2) {
        sol = mid;
        right = mid - 1;
        continue;
      } else
        left = mid + 1;
    }
  }
  return sol;
}
void add(int node, int val) {
  for (int i = 0; i < G[node].size(); i++) {
    int neighb = G[node][i];
    add(neighb, val);
  }
  S[0][DP[node]] += val;
  S[2][DP[node]] -= val;
  if (S[2][DP[node]] == 0) S[2].erase(DP[node]);
  if (S[0][DP[node]] == 0) S[0].erase(DP[node]);
}
void DFS(int node, bool keep) {
  S[1][DP[node]]++;
  if (S[2][DP[node]] == 1)
    S[2].erase(DP[node]);
  else
    S[2][DP[node]]--;
  int Max = 0, PMax = 0, cnt = 0, Min = 0x3f3f3f3f;
  for (int i = 0; i < G[node].size(); i++) {
    int neighb = G[node][i];
    if (Max == DP[neighb]) cnt++;
    if (Max < DP[neighb]) {
      Max = DP[neighb];
      PMax = neighb;
      cnt = 1;
    }
    Min = min(Min, DP[neighb]);
  }
  for (int i = 0; i < G[node].size(); i++) {
    int neighb = G[node][i];
    if (neighb == PMax) continue;
    DFS(neighb, 0);
  }
  Min = min(Min, N - DP[node]);
  if (PMax != 0) {
    DFS(PMax, 1);
  }
  if (N - DP[node] < Max) Res[node] = findS(node, 0);
  for (int i = 0; i < G[node].size(); i++) {
    int neighb = G[node][i];
    if (neighb == PMax) continue;
    add(neighb, 1);
  }
  if (N - DP[node] >= Max) Res[node] = findS(node, 1);
  if (keep == 1) {
    S[1][DP[node]]--;
    S[0][DP[node]]++;
    if (S[1][DP[node]] == 0) S[1].erase(DP[node]);
  } else {
    for (int i = 0; i < G[node].size(); i++) {
      int neighb = G[node][i];
      add(neighb, -1);
    }
    S[1][DP[node]]--;
    S[2][DP[node]]++;
    if (S[1][DP[node]] == 0) S[1].erase(DP[node]);
  }
}
int main() {
  Read();
  precalcSize(Root);
  for (int i = 1; i <= N; i++) S[2][DP[i]]++;
  DFS(Root, 1);
  for (int i = 1; i <= N; i++) cout << Res[i] << "\n";
  return 0;
}
