For problem statement at 0-999/400-499/440-449/442/problemD.txt this is a correct solution, but verifier at 0-999/400-499/440-449/442/verifierD.go ends with case 1 failed: expected 1 1 1 2 3 4 4 4
got 1 1 1 1 1 1 1 1input:
8
1 1 3 4 5 6 2 8
exit status 1 can you fix the verifier? package main
import (
"io"
"os"
)
func nextInt(data []byte, idx *int) int {
n := len(data)
i := *idx
for i < n && (data[i] < '0' || data[i] > '9') {
i++
}
v := 0
for i < n && data[i] >= '0' && data[i] <= '9' {
v = v*10 + int(data[i]-'0')
i++
}
*idx = i
return v
}
func appendInt(out []byte, x int) []byte {
if x >= 100 {
out = append(out, byte('0'+x/100))
x %= 100
out = append(out, byte('0'+x/10), byte('0'+x%10))
} else if x >= 10 {
out = append(out, byte('0'+x/10), byte('0'+x%10))
} else {
out = append(out, byte('0'+x))
}
return out
}
func main() {
data, _ := io.ReadAll(os.Stdin)
idx := 0
n := nextInt(data, &idx)
size := n + 2
parent := make([]int, size)
h := make([]uint8, size)
maxChild := make([]uint8, size)
countMax := make([]uint32, size)
out := make([]byte, 0, n*4)
for i := 1; i <= n; i++ {
p := nextInt(data, &idx)
v := i + 1
parent[v] = p
oldH := h[p]
changed := false
if countMax[p] == 0 {
countMax[p] = 1
changed = true
} else if maxChild[p] == 0 {
countMax[p]++
changed = true
}
if changed {
newH := maxChild[p]
if countMax[p] >= 2 {
newH++
}
if newH != oldH {
h[p] = newH
child := p
oldVal := oldH
newVal := newH
for {
par := parent[child]
if par == 0 {
break
}
oldParH := h[par]
mp := maxChild[par]
if oldVal == mp {
maxChild[par] = newVal
countMax[par] = 1
} else if newVal == mp {
countMax[par]++
} else {
break
}
newParH := maxChild[par]
if countMax[par] >= 2 {
newParH++
}
if newParH == oldParH {
break
}
h[par] = newParH
child = par
oldVal = oldParH
newVal = newParH
}
}
}
ans := 0
if countMax[1] != 0 {
ans = int(maxChild[1]) + 1
}
if i > 1 {
out = append(out, ' ')
}
out = appendInt(out, ans)
}
os.Stdout.Write(out)
}