For problem statement at 1000-1999/1600-1699/1680-1689/1689/problemE.txt this is a correct solution, but verifier at 1000-1999/1600-1699/1680-1689/1689/verifierE.go ends with test 12 failed
input:
1
2
17 4
expected: 1
17 5
got: 1
17 3
exit status 1 can you fix the verifier? ```go
package main
import (
"bufio"
"fmt"
"math/bits"
"os"
)
type dsu struct {
p []int
s []int
}
func newDsu(n int) *dsu {
p := make([]int, n)
s := make([]int, n)
for i := 0; i < n; i++ {
p[i] = i
s[i] = 1
}
return &dsu{p: p, s: s}
}
func (d *dsu) find(x int) int {
for x != d.p[x] {
d.p[x] = d.p[d.p[x]]
x = d.p[x]
}
return x
}
func (d *dsu) union(a, b int) {
ra := d.find(a)
rb := d.find(b)
if ra == rb {
return
}
if d.s[ra] < d.s[rb] {
ra, rb = rb, ra
}
d.p[rb] = ra
d.s[ra] += d.s[rb]
}
func connected(a []int) bool {
n := len(a)
start := -1
for i := 0; i < n; i++ {
if a[i] != 0 {
start = i
break
}
}
if start == -1 {
return false
}
d := newDsu(n)
for b := 0; b < 30; b++ {
prev := -1
for i := 0; i < n; i++ {
if ((a[i] >> b) & 1) != 0 {
if prev == -1 {
prev = i
} else {
d.union(prev, i)
}
}
}
}
root := d.find(start)
for i := 0; i < n; i++ {
if a[i] != 0 && d.find(i) != root {
return false
}
if a[i] == 0 {
return false
}
}
return true
}
func main() {
in := bufio.NewReader(os.Stdin)
out := bufio.NewWriter(os.Stdout)
defer out.Flush()
var t int
fmt.Fscan(in, &t)
for ; t > 0; t-- {
var n int
fmt.Fscan(in, &n)
a := make([]int, n)
for i := 0; i < n; i++ {
fmt.Fscan(in, &a[i])
}
ops := 0
for i := 0; i < n; i++ {
if a[i] == 0 {
a[i] = 1
ops++
}
}
if connected(a) {
fmt.Fprintln(out, ops)
for i := 0; i < n; i++ {
if i > 0 {
fmt.Fprint(out, " ")
}
fmt.Fprint(out, a[i])
}
fmt.Fprintln(out)
continue
}
done := false
for i := 0; i < n && !done; i++ {
x := a[i]
if x > 1 {
a[i] = x - 1
if connected(a) {
fmt.Fprintln(out, ops+1)
for j := 0; j < n; j++ {
if j > 0 {
fmt.Fprint(out, " ")
}
fmt.Fprint(out, a[j])
}
fmt.Fprintln(out)
done = true
}
a[i] = x
if done {
break
}
}
a[i] = x + 1
if connected(a) {
fmt.Fprintln(out, ops+1)
for j := 0; j < n; j++ {
if j > 0 {
fmt.Fprint(out, " ")
}
fmt.Fprint(out, a[j])
}
fmt.Fprintln(out)
done = true
}
a[i] = x
}
if done {
continue
}
maxTZ := -1
idxs := make([]int, 0)
for i := 0; i < n; i++ {
tz := bits.TrailingZeros(uint(a[i]))
if tz > maxTZ {
maxTZ = tz
idxs = idxs[:0]
idxs = append(idxs, i)
} else if tz == maxTZ {
idxs = append(idxs, i)
}
}
i := idxs[0]
j := idxs[1]
a[i]--
a[j]++
fmt.Fprintln(out, ops+2)
for k := 0; k < n; k++ {
if k > 0 {
fmt.Fprint(out, " ")
}
fmt.Fprint(out, a[k])
}
fmt.Fprintln(out)
}
}
```