← Home
For problem statement at 1000-1999/1800-1899/1820-1829/1827/problemD.txt this is a correct solution, but verifier at 1000-1999/1800-1899/1820-1829/1827/verifierD.go ends with All tests passed can you fix the verifier? package main

import (
	"bufio"
	"os"
	"strconv"
)

type BIT []int

func NewBIT(n int) BIT {
	return make(BIT, n+2)
}

func (b BIT) add(idx, val int) {
	for ; idx < len(b); idx += idx & -idx {
		b[idx] += val
	}
}

func (b BIT) query(idx int) int {
	sum := 0
	for ; idx > 0; idx -= idx & -idx {
		sum += b[idx]
	}
	return sum
}

type SegTree struct {
	tree []int
	n    int
}

func NewSegTree(n int) *SegTree {
	if n == 0 {
		return &SegTree{tree: make([]int, 0), n: 0}
	}
	return &SegTree{tree: make([]int, 2*n), n: n}
}

func (st *SegTree) update(idx, val int) {
	if st.n == 0 {
		return
	}
	idx += st.n
	st.tree[idx] = val
	for idx > 1 {
		idx /= 2
		a, b := st.tree[2*idx], st.tree[2*idx+1]
		if a > b {
			st.tree[idx] = a
		} else {
			st.tree[idx] = b
		}
	}
}

func (st *SegTree) query(l, r int) int {
	if st.n == 0 || l > r {
		return 0
	}
	res := 0
	l += st.n
	r += st.n
	for l <= r {
		if l%2 == 1 {
			if st.tree[l] > res {
				res = st.tree[l]
			}
			l++
		}
		if r%2 == 0 {
			if st.tree[r] > res {
				res = st.tree[r]
			}
			r--
		}
		l /= 2
		r /= 2
	}
	return res
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]byte, 1024*1024), 1024*1024*10)
	scanner.Split(bufio.ScanWords)

	nextInt := func() int {
		scanner.Scan()
		s := scanner.Bytes()
		res := 0
		for _, b := range s {
			res = res*10 + int(b-'0')
		}
		return res
	}

	if !scanner.Scan() {
		return
	}
	s := scanner.Bytes()
	t := 0
	for _, b := range s {
		t = t*10 + int(b-'0')
	}

	out_buf := bufio.NewWriterSize(os.Stdout, 1024*1024)
	defer out_buf.Flush()

	for tc := 0; tc < t; tc++ {
		n := nextInt()
		parent := make([]int, n+1)
		adj := make([][]int, n+1)
		for i := 2; i <= n; i++ {
			parent[i] = nextInt()
			adj[parent[i]] = append(adj[parent[i]], i)
		}

		sz := make([]int, n+1)
		heavy_child := make([]int, n+1)
		for i := n; i >= 1; i-- {
			sz[i]++
			p := parent[i]
			if p != 0 {
				sz[p] += sz[i]
				if heavy_child[p] == 0 || sz[i] > sz[heavy_child[p]] {
					heavy_child[p] = i
				}
			}
		}

		top := make([]int, n+1)
		in := make([]int, n+1)
		out := make([]int, n+1)
		timer := 0

		var dfs func(u, t_val int)
		dfs = func(u, t_val int) {
			top[u] = t_val
			timer++
			in[u] = timer
			if heavy_child[u] != 0 {
				dfs(heavy_child[u], t_val)
			}
			for _, v := range adj[u] {
				if v != heavy_child[u] {
					dfs(v, v)
				}
			}
			out[u] = timer
		}
		dfs(1, 1)

		light_start := make([]int, n+1)
		light_end := make([]int, n+1)
		light_pos := make([]int, n+1)
		light_timer := 0

		for i := 1; i <= n; i++ {
			start := light_timer
			for _, v := range adj[i] {
				if v != heavy_child[i] {
					light_pos[v] = light_timer
					light_timer++
				}
			}
			light_start[i] = start
			light_end[i] = light_timer - 1
		}

		segtree := NewSegTree(light_timer)
		bit := NewBIT(n)

		update_HLD := func(x int) {
			u := x
			for u != 0 {
				top_u := top[u]
				p := parent[top_u]

				bit.add(in[top_u], 1)
				bit.add(in[u]+1, -1)

				if p != 0 {
					new_size := bit.query(in[top_u])
					segtree.update(light_pos[top_u], new_size)
				}
				u = p
			}
		}

		get_child_ancestor := func(C, x int) int {
			u := x
			last := 0
			for top[u] != top[C] {
				last = top[u]
				u = parent[top[u]]
			}
			if u == C {
				return last
			}
			return heavy_child[C]
		}

		n_active := 1
		C := 1
		update_HLD(1)

		ans := make([]int, n-1)
		for i := 2; i <= n; i++ {
			n_active++
			update_HLD(i)

			branch := 0
			if C != i {
				if in[C] <= in[i] && in[i] <= out[C] {
					branch = get_child_ancestor(C, i)
				} else {
					branch = parent[C]
				}
			}

			if branch != 0 {
				sz_val := 0
				if branch == parent[C] {
					sz_val = n_active - bit.query(in[C])
				} else {
					sz_val = bit.query(in[branch])
				}

				if sz_val > n_active/2 {
					C = branch
				}
			}

			max_b := 0
			if parent[C] != 0 {
				v := n_active - bit.query(in[C])
				if v > max_b {
					max_b = v
				}
			}
			if heavy_child[C] != 0 {
				v := bit.query(in[heavy_child[C]])
				if v > max_b {
					max_b = v
				}
			}
			if light_start[C] <= light_end[C] {
				v := segtree.query(light_start[C], light_end[C])
				if v > max_b {
					max_b = v
				}
			}

			ans[i-2] = n_active - 2*max_b
		}

		for i := 0; i < n-1; i++ {
			if i > 0 {
				out_buf.WriteByte(' ')
			}
			out_buf.WriteString(strconv.Itoa(ans[i]))
		}
		out_buf.WriteByte('\n')
	}
}