inlineintrd(){ int a = 1, b = 0; char c = getchar(); while (!isdigit(c)) a = c == '-' ? 0 : 1, c = getchar(); while (isdigit(c)) b = b * 10 + c - '0', c = getchar(); return a ? b : -b; }
constint N = 1e6 + 233;
int n, dep[N], son[N], *f[N], tmp[N], *pt = tmp, ans[N]; std::vector<int> G[N];
voiddfs1(int x, int fa){ for (int y : G[x]) if (y != fa) { dfs1(y, x); if (dep[y] > dep[son[x]]) son[x] = y; } dep[x] = dep[son[x]] + 1; }
voiddfs2(int x, int fa){ f[x][0] = 1; if (son[x]) { f[son[x]] = f[x] + 1; dfs2(son[x], x); ans[x] = ans[son[x]] + 1; } for (int y : G[x]) if (y != fa && y != son[x]) { f[y] = pt; pt += dep[y]; dfs2(y, x); for (int j = 1; j <= dep[y]; ++j) { f[x][j] += f[y][j - 1]; if ((j < ans[x] && f[x][j] >= f[x][ans[x]]) (j > ans[x] && f[x][j] > f[x][ans[x]])) ans[x] = j; } } if (f[x][ans[x]] == 1) ans[x] = 0; }
intmain(){ n = rd(); for (int i = 1; i < n; ++i) { int x = rd(), y = rd(); G[x].push_back(y); G[y].push_back(x); } dfs1(1, 0); f[1] = pt; pt += dep[1]; dfs2(1, 0); for (int i = 1; i <= n; ++i) printf("%d\n", ans[i]); return0; }
inlineintrd(){ int a = 1, b = 0; char c = getchar(); while (!isdigit(c)) a = c == '-' ? 0 : 1, c = getchar(); while (isdigit(c)) b = b * 10 + c - '0', c = getchar(); return a ? b : -b; }
constint N = 1e5 + 233;
int n, len[N], son[N];
structGraph { int to, nxt; } G[N * 2];
int head[N], tot;
inlinevoidaddedge(int x, int y){ G[++tot].to = y, G[tot].nxt = head[x], head[x] = tot; }
voiddfs1(int x, int fa){ for (int i = head[x]; i; i = G[i].nxt) { int y = G[i].to; if (y != fa) { dfs1(y, x); if (len[y] > len[son[x]]) son[x] = y; } } len[x] = len[son[x]] + 1; }