For problem statement at 1000-1999/1300-1399/1310-1319/1311/problemE.txt this is a correct solution, but verifier at 1000-1999/1300-1399/1310-1319/1311/verifierE.go ends with test 6 failed
expected:
YES
1 2 2 3 3 5 5 6 6
got:
YES
1 1 2 2 4 4 6 8 9
exit status 1 can you fix the verifier? ```go
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
in := bufio.NewReader(os.Stdin)
out := bufio.NewWriter(os.Stdout)
defer out.Flush()
var t int
if _, err := fmt.Fscan(in, &t); err != nil {
return
}
for i := 0; i < t; i++ {
var n, d int
fmt.Fscan(in, &n, &d)
solve(out, n, d)
}
}
func solve(out *bufio.Writer, n, d int) {
// Maximum sum of depths is when the tree is a path (0, 1, ..., n-1)
maxSum := n * (n - 1) / 2
if d > maxSum {
fmt.Fprintln(out, "NO")
return
}
// Minimum sum of depths is when the tree is a complete binary tree
minSum := 0
rem := n
width := 1
depth := 0
for rem > 0 {
take := width
if rem < take {
take = rem
}
minSum += take * depth
rem -= take
width *= 2
depth++
}
if d < minSum {
fmt.Fprintln(out, "NO")
return
}
fmt.Fprintln(out, "YES")
// Start with a chain: 0-1-2-...-(n-1)
// cnt[i] is the number of nodes at depth i
cnt := make([]int, n)
for i := range cnt {
cnt[i] = 1
}
currentSum := maxSum
deepest := n - 1 // The deepest level that currently has a node
target := 1 // The shallowest level that can accept a child
for currentSum > d {
// Find the first level 'target' that is not full.
// A level h is not full if cnt[h] < 2 * cnt[h-1].
for target < deepest && cnt[target] == 2*cnt[target-1] {
target++
}
// Calculate how much we can reduce the sum by moving a node from 'deepest' to 'target'
move := deepest - target
// Calculate how much we NEED to reduce
diff := currentSum - d
if move > diff {
// We need less than the max possible move.
// Move the node from 'deepest' to 'deepest - diff'
finalDepth := deepest - diff
cnt[deepest]--
cnt[finalDepth]++
currentSum -= diff
break
} else {
// Move the node from 'deepest' to 'target'
cnt[deepest]--
cnt[target]++
currentSum -= move
// If we removed the last node from 'deepest', update 'deepest'
if cnt[deepest] == 0 {
deepest--
}
}
}
// Construct parents array from cnt distribution
parents := make([]int, n+1)
layer := []int{1} // Vertex IDs at the current level
nextID := 2
for h := 1; h < n; h++ {
if cnt[h] == 0 {
break
}
numNodes := cnt[h]
newLayer := make([]int, 0, numNodes)
parentIdx := 0
degree := 0 // Number of children assigned to current parent
for i := 0; i < numNodes; i++ {
p := layer[parentIdx]
parents[nextID] = p
newLayer = append(newLayer, nextID)
nextID++
degree++
if degree == 2 {
degree = 0
parentIdx++
}
}
layer = newLayer
}
// Print parents for vertices 2 to n
for i := 2; i <= n; i++ {
if i > 2 {
fmt.Fprint(out, " ")
}
fmt.Fprint(out, parents[i])
}
fmt.Fprintln(out)
}
```