#include <bits/stdc++.h>
using namespace std;
int n, u, f, root, rt_all, rt_path, rt[100005], fa[100005], ans[100005];
int read() {
  int x = 0;
  char ch = getchar();
  while (!isdigit(ch)) ch = getchar();
  while (isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
  return x;
}
namespace SBT {
double alpha = 0.55;
int lc[400005], rc[400005], tot;
int val[400005], c[400005], siz[400005], rsiz[400005];
int dl[400005], tp, q[400005], qc[400005], qn;
int newnode(int v, int ct) {
  int u = tp ? dl[tp--] : ++tot;
  lc[u] = rc[u] = 0, val[u] = v, c[u] = siz[u] = ct, rsiz[u] = 1;
  return u;
}
void pushup(int u) {
  rsiz[u] = rsiz[lc[u]] + rsiz[rc[u]] + 1;
  siz[u] = siz[lc[u]] + siz[rc[u]] + c[u];
}
void Zig(int &u) {
  int v = lc[u];
  lc[u] = rc[v], rc[v] = u;
  pushup(u), pushup(v);
  u = v;
}
void Zag(int &u) {
  int v = rc[u];
  rc[u] = lc[v], lc[v] = u;
  pushup(u), pushup(v);
  u = v;
}
void ins(int &u, int v, int ct = 1) {
  if (!u) {
    u = newnode(v, ct);
    return;
  }
  siz[u] += ct;
  if (val[u] == v)
    c[u] += ct;
  else if (v < val[u]) {
    ins(lc[u], v, ct);
    if (rsiz[lc[u]] > rsiz[u] * alpha) Zig(u);
  } else {
    ins(rc[u], v, ct);
    if (rsiz[rc[u]] > rsiz[u] * alpha) Zag(u);
  }
  pushup(u);
}
void del(int &u, int v) {
  if (!u) return;
  if (val[u] == v) {
    if (c[u] > 1)
      --c[u], --siz[u];
    else if (!lc[u] || !rc[u])
      dl[++tp] = u, u = lc[u] | rc[u];
    else if (rsiz[lc[u]] < rsiz[rc[u]])
      Zag(u), del(u, v);
    else
      Zig(u), del(u, v);
    if (u) pushup(u);
    return;
  }
  if (v < val[u])
    del(lc[u], v);
  else
    del(rc[u], v);
  pushup(u);
}
int Rank(int u, int v, int ans = 0) {
  while (u) {
    if (val[u] == v) return ans + siz[lc[u]] + c[u];
    if (val[u] < v)
      ans += siz[lc[u]] + c[u], u = rc[u];
    else
      u = lc[u];
  }
  return ans;
}
int range(int rt, int l, int r) { return Rank(rt, r) - Rank(rt, l - 1); }
void inorder_dfs(int u) {
  if (lc[u]) inorder_dfs(lc[u]);
  q[++qn] = val[u], qc[qn] = c[u], dl[++tp] = u;
  if (rc[u]) inorder_dfs(rc[u]);
}
void Join(int &u, int &v) {
  if (rsiz[u] < rsiz[v]) std::swap(u, v);
  qn = 0, inorder_dfs(v), v = 0;
  for (int i = 1; i <= qn; ++i) ins(u, q[i], qc[i]);
}
}  // namespace SBT
using SBT::del;
using SBT::ins;
using SBT::Join;
using SBT::range;
namespace GRAPH {
int Head[100005], Next[100005 << 1], V[100005 << 1], siz[100005], cnt = 0;
void add(int u, int v) { Next[++cnt] = Head[u], V[cnt] = v, Head[u] = cnt; }
void pre_dfs(int u) {
  siz[u] = 1;
  for (int i = Head[u], v = V[i]; i; v = V[i = Next[i]])
    if (v != fa[u]) pre_dfs(v), siz[u] += siz[v];
}
bool check(int u, int mxson, int mn, int mx, int lim) {
  int l = mx - lim, r = lim - mn;
  if (l > r) return false;
  if (mxson == fa[u])
    return (range(rt_all, l, r) - range(rt[u], l, r) +
            range(rt_path, l + siz[u], r + siz[u])) > 0;
  else
    return range(rt[mxson], l, r) > 0;
}
void solve(int u) {
  del(rt_all, siz[u]);
  if (u != root) ins(rt_path, siz[u]);
  int mn = n + 1, mx = 0, submx = 0, mxson = 0;
  for (int i = Head[u], v = V[i]; i; v = V[i = Next[i]]) {
    int siz_v = v == fa[u] ? n - siz[u] : siz[v];
    if (v != fa[u]) solve(v);
    mn = min(mn, siz_v);
    if (siz_v > mx)
      submx = mx, mxson = v, mx = siz_v;
    else if (siz_v > submx)
      submx = siz_v;
  }
  ins(rt_all, siz[u]), ins(rt[u], siz[u]);
  if (u != root) del(rt_path, siz[u]);
  if (mxson == fa[u])
    for (int i = Head[u], v = V[i]; i; v = V[i = Next[i]])
      if (v != fa[u]) Join(rt[u], rt[v]);
  int l = submx == 0 ? mx : submx, r = mx, mid;
  while (l < r)
    check(u, mxson, mn, mx, mid = l + r >> 1) ? r = mid : l = mid + 1;
  ans[u] = l;
  if (mxson != fa[u])
    for (int i = Head[u], v = V[i]; i; v = V[i = Next[i]])
      if (v != fa[u]) Join(rt[u], rt[v]);
}
}  // namespace GRAPH
using GRAPH::add;
int main() {
  n = read();
  for (int i = 1; i <= n; ++i) {
    f = read(), u = read();
    if (!f)
      root = u;
    else
      add(fa[u] = f, u), add(u, f);
  }
  GRAPH::pre_dfs(root);
  for (int i = 1; i <= n; ++i) ins(rt_all, GRAPH::siz[i]);
  GRAPH::solve(root);
  for (int i = 1; i <= n; ++i) printf("%d\n", ans[i]);
}

### Prompt

Please provide a cpp coded solution to the problem described below:

Given a rooted tree with n nodes. The Night King removes exactly one node from the tree and all the edges associated with it. Doing this splits the tree and forms a forest. The node which is removed is not a part of the forest.

The root of a tree in the forest is the node in that tree which does not have a parent. We define the strength of the forest as the size of largest tree in forest.

Jon Snow wants to minimize the strength of the forest. To do this he can perform the following operation at most once.

He removes the edge between a node and its parent and inserts a new edge between this node and any other node in forest such that the total number of trees in forest remain same.

For each node v you need to find the minimum value of strength of the forest formed when node v is removed.

Input

The first line of the input contains an integer n (1 ≤ n ≤ 105) — the number of vertices in the tree. Each of the next n lines contains a pair of vertex indices ui and vi (1 ≤ ui, vi ≤ n) where ui is the parent of vi. If ui = 0 then vi is the root.

Output

Print n line each containing a single integer. The i-th of them should be equal to minimum value of strength of forest formed when i-th node is removed and Jon Snow performs the operation described above at most once.

Examples

Input

10
0 1
1 2
1 3
1 4
2 5
2 6
3 7
4 8
4 9
5 10


Output

3
4
5
5
5
9
9
9
9
9


Input

2
2 1
0 2


Output

1
1

Note

The tree for first test case is depicted below. <image> When you remove the first node, the tree splits to form the following forest. The strength of this forest is 4. <image> Jon Snow now changes the parent of vertex 10 from 5 to 3. The strength of forest now becomes 3. <image>

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
int n, u, f, root, rt_all, rt_path, rt[100005], fa[100005], ans[100005];
int read() {
  int x = 0;
  char ch = getchar();
  while (!isdigit(ch)) ch = getchar();
  while (isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
  return x;
}
namespace SBT {
double alpha = 0.55;
int lc[400005], rc[400005], tot;
int val[400005], c[400005], siz[400005], rsiz[400005];
int dl[400005], tp, q[400005], qc[400005], qn;
int newnode(int v, int ct) {
  int u = tp ? dl[tp--] : ++tot;
  lc[u] = rc[u] = 0, val[u] = v, c[u] = siz[u] = ct, rsiz[u] = 1;
  return u;
}
void pushup(int u) {
  rsiz[u] = rsiz[lc[u]] + rsiz[rc[u]] + 1;
  siz[u] = siz[lc[u]] + siz[rc[u]] + c[u];
}
void Zig(int &u) {
  int v = lc[u];
  lc[u] = rc[v], rc[v] = u;
  pushup(u), pushup(v);
  u = v;
}
void Zag(int &u) {
  int v = rc[u];
  rc[u] = lc[v], lc[v] = u;
  pushup(u), pushup(v);
  u = v;
}
void ins(int &u, int v, int ct = 1) {
  if (!u) {
    u = newnode(v, ct);
    return;
  }
  siz[u] += ct;
  if (val[u] == v)
    c[u] += ct;
  else if (v < val[u]) {
    ins(lc[u], v, ct);
    if (rsiz[lc[u]] > rsiz[u] * alpha) Zig(u);
  } else {
    ins(rc[u], v, ct);
    if (rsiz[rc[u]] > rsiz[u] * alpha) Zag(u);
  }
  pushup(u);
}
void del(int &u, int v) {
  if (!u) return;
  if (val[u] == v) {
    if (c[u] > 1)
      --c[u], --siz[u];
    else if (!lc[u] || !rc[u])
      dl[++tp] = u, u = lc[u] | rc[u];
    else if (rsiz[lc[u]] < rsiz[rc[u]])
      Zag(u), del(u, v);
    else
      Zig(u), del(u, v);
    if (u) pushup(u);
    return;
  }
  if (v < val[u])
    del(lc[u], v);
  else
    del(rc[u], v);
  pushup(u);
}
int Rank(int u, int v, int ans = 0) {
  while (u) {
    if (val[u] == v) return ans + siz[lc[u]] + c[u];
    if (val[u] < v)
      ans += siz[lc[u]] + c[u], u = rc[u];
    else
      u = lc[u];
  }
  return ans;
}
int range(int rt, int l, int r) { return Rank(rt, r) - Rank(rt, l - 1); }
void inorder_dfs(int u) {
  if (lc[u]) inorder_dfs(lc[u]);
  q[++qn] = val[u], qc[qn] = c[u], dl[++tp] = u;
  if (rc[u]) inorder_dfs(rc[u]);
}
void Join(int &u, int &v) {
  if (rsiz[u] < rsiz[v]) std::swap(u, v);
  qn = 0, inorder_dfs(v), v = 0;
  for (int i = 1; i <= qn; ++i) ins(u, q[i], qc[i]);
}
}  // namespace SBT
using SBT::del;
using SBT::ins;
using SBT::Join;
using SBT::range;
namespace GRAPH {
int Head[100005], Next[100005 << 1], V[100005 << 1], siz[100005], cnt = 0;
void add(int u, int v) { Next[++cnt] = Head[u], V[cnt] = v, Head[u] = cnt; }
void pre_dfs(int u) {
  siz[u] = 1;
  for (int i = Head[u], v = V[i]; i; v = V[i = Next[i]])
    if (v != fa[u]) pre_dfs(v), siz[u] += siz[v];
}
bool check(int u, int mxson, int mn, int mx, int lim) {
  int l = mx - lim, r = lim - mn;
  if (l > r) return false;
  if (mxson == fa[u])
    return (range(rt_all, l, r) - range(rt[u], l, r) +
            range(rt_path, l + siz[u], r + siz[u])) > 0;
  else
    return range(rt[mxson], l, r) > 0;
}
void solve(int u) {
  del(rt_all, siz[u]);
  if (u != root) ins(rt_path, siz[u]);
  int mn = n + 1, mx = 0, submx = 0, mxson = 0;
  for (int i = Head[u], v = V[i]; i; v = V[i = Next[i]]) {
    int siz_v = v == fa[u] ? n - siz[u] : siz[v];
    if (v != fa[u]) solve(v);
    mn = min(mn, siz_v);
    if (siz_v > mx)
      submx = mx, mxson = v, mx = siz_v;
    else if (siz_v > submx)
      submx = siz_v;
  }
  ins(rt_all, siz[u]), ins(rt[u], siz[u]);
  if (u != root) del(rt_path, siz[u]);
  if (mxson == fa[u])
    for (int i = Head[u], v = V[i]; i; v = V[i = Next[i]])
      if (v != fa[u]) Join(rt[u], rt[v]);
  int l = submx == 0 ? mx : submx, r = mx, mid;
  while (l < r)
    check(u, mxson, mn, mx, mid = l + r >> 1) ? r = mid : l = mid + 1;
  ans[u] = l;
  if (mxson != fa[u])
    for (int i = Head[u], v = V[i]; i; v = V[i = Next[i]])
      if (v != fa[u]) Join(rt[u], rt[v]);
}
}  // namespace GRAPH
using GRAPH::add;
int main() {
  n = read();
  for (int i = 1; i <= n; ++i) {
    f = read(), u = read();
    if (!f)
      root = u;
    else
      add(fa[u] = f, u), add(u, f);
  }
  GRAPH::pre_dfs(root);
  for (int i = 1; i <= n; ++i) ins(rt_all, GRAPH::siz[i]);
  GRAPH::solve(root);
  for (int i = 1; i <= n; ++i) printf("%d\n", ans[i]);
}

```