← Home
For problem statement at 0-999/300-399/300-309/309/problemE.txt this is a correct solution, but verifier at 0-999/300-399/300-309/309/verifierE.go ends with case 1: runtime error: exit status 2
panic: runtime error: index out of range [1656] with length 7

goroutine 1 [running]:
main.main()
	/tmp/build-1501072593/solution.go:41 +0x804

input:
6
894 1656
960 1557
874 1048
722 1374
993 1222
552 632
exit status 1 can you fix the verifier? package main

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

type Item struct {
	v   int
	rem int
}

func main() {
	data, _ := io.ReadAll(os.Stdin)
	idx := 0
	readInt := func() int {
		for idx < len(data) && (data[idx] < '0' || data[idx] > '9') && data[idx] != '-' {
			idx++
		}
		sign := 1
		if data[idx] == '-' {
			sign = -1
			idx++
		}
		n := 0
		for idx < len(data) && data[idx] >= '0' && data[idx] <= '9' {
			n = n*10 + int(data[idx]-'0')
			idx++
		}
		return sign * n
	}

	n := readInt()
	m := readInt()

	children := make([][]int, n+1)
	for i := 2; i <= n; i++ {
		p := readInt()
		children[p] = append(children[p], i)
	}

	init := make([]int, n+1)
	for i := 1; i <= n; i++ {
		init[i] = -1
	}

	for i := 0; i < m; i++ {
		x := readInt()
		y := readInt()
		if y > init[x] {
			init[x] = y
		}
	}

	stack := make([]Item, 1, n)
	stack[0] = Item{1, -1}
	ans := 0

	for len(stack) > 0 {
		it := stack[len(stack)-1]
		stack = stack[:len(stack)-1]

		rem := it.rem
		if init[it.v] > rem {
			rem = init[it.v]
		}
		if rem >= 0 {
			ans++
		}

		nextRem := rem - 1
		for _, ch := range children[it.v] {
			stack = append(stack, Item{ch, nextRem})
		}
	}

	w := bufio.NewWriter(os.Stdout)
	w.WriteString(strconv.Itoa(ans))
	w.WriteByte('\n')
	w.Flush()
}