← Home
```go
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n int
	fmt.Fscan(in, &n)

	adj := make([][]int, n+1)
	for i := 0; i < n-1; i++ {
		var x, y int
		fmt.Fscan(in, &x, &y)
		adj[x] = append(adj[x], y)
		adj[y] = append(adj[y], x)
	}

	size := make([]int, n+1)
	dfsSize(1, 0, adj, size)

	dom := make([]int, n+1)
	profile := make([]Profile, n+1)
	dfsCompute(1, 0, adj, size, dom, profile)

	for i := 1; i <= n; i++ {
		fmt.Fprint(out, dom[i], " ")
	}
	fmt.Fprintln(out)
}

type Profile struct {
	counts map[int]int
	shift  int
	ms     int
	minl   int
}

func dfsSize(u, par int, adj [][]int, size []int) int {
	s := 1
	for _, v := range adj[u] {
		if v != par {
			s += dfsSize(v, u, adj, size)
		}
	}
	size[u] = s
	return s
}

func dfsCompute(u, par int, adj [][]int, size []int, dom []int, profile []Profile) {
	children := []int{}
	for _, v := range adj[u] {
		if v != par {
			dfsCompute(v, u, adj, size, dom, profile)
			children = append(children, v)
		}
	}

	m := len(children)
	if m == 0 {
		profile[u].counts = map[int]int{0: 1}
		profile[u].shift = 0
		profile[u].ms = 1
		profile[u].minl = 0
		dom[u] = 0
		return
	}
	if m == 1 {
		c := children[0]
		profile[u].counts = profile[c].counts
		profile[u].shift = profile[c].shift + 1
		profile[u].ms = max(1, profile[c].ms)
		profile[u].minl = 0
		if profile[c].ms > 1 {
			profile[u].minl = 1 + profile[c].minl
		}
		key := 0 - profile[u].shift
		profile[u].counts[key] = 1
		dom[u] = profile[u].minl
		return
	}

	// m >= 2
	heavy := -1
	maxsz := -1
	for _, c := range children {
		if size[c] > maxsz {
			maxsz = size[c]
			heavy = c
		}
	}

	var S Profile
	S.counts = profile[heavy].counts
	S.shift = profile[heavy].shift
	S.ms = profile[heavy].ms
	S.minl = profile[heavy].minl

	for _, c := range children {
		if c == heavy {
			continue
		}
		// add c to S
		max_updated := 0
		min_k_for_max_updated := 1 << 30
		found_equal := false
		min_k_equal := 1 << 30
		for key, v := range profile[c].counts {
			small_d := key + profile[c].shift
			large_key := small_d - S.shift
			old_val := 0
			if val, ok := S.counts[large_key]; ok {
				old_val = val
			}
			new_val := old_val + v
			S.counts[large_key] = new_val
			if new_val > max_updated {
				max_updated = new_val
				min_k_for_max_updated = small_d
			} else if new_val == max_updated {
				if small_d < min_k_for_max_updated {
					min_k_for_max_updated = small_d
				}
			}
			if new_val == S.ms {
				if !found_equal || small_d < min_k_equal {
					min_k_equal = small_d
					found_equal = true
				}
			}
		}
		// update S
		if max_updated > S.ms {
			S.ms = max_updated
			S.minl = min_k_for_max_updated
		} else if max_updated == S.ms {
			if found_equal {
				S.minl = min(S.minl, min_k_equal)
			}
		}
	}

	dom[u] = 0
	if S.ms > 1 {
		dom[u] = 1 + S.minl
	}

	profile[u].counts = S.counts
	profile[u].shift = S.shift + 1
	profile[u].ms = max(1, S.ms)
	profile[u].minl = 0
	if S.ms > 1 {
		profile[u].minl = 1 + S.minl
	}
	key := 0 - profile[u].shift
	profile[u].counts[key] = 1
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
```