For problem statement at 1000-1999/1200-1299/1240-1249/1244/problemD.txt this is a correct solution, but verifier at 1000-1999/1200-1299/1240-1249/1244/verifierD.go ends with all tests passed can you fix the verifier? package main
import (
"bytes"
"fmt"
"io"
"os"
"strconv"
)
func main() {
buf, _ := io.ReadAll(os.Stdin)
pos := 0
nextInt := func() int {
for pos < len(buf) && buf[pos] <= ' ' {
pos++
}
if pos >= len(buf) {
return 0
}
res := 0
for pos < len(buf) && buf[pos] > ' ' {
res = res*10 + int(buf[pos]-'0')
pos++
}
return res
}
nextInt64 := func() int64 {
for pos < len(buf) && buf[pos] <= ' ' {
pos++
}
if pos >= len(buf) {
return 0
}
var res int64
for pos < len(buf) && buf[pos] > ' ' {
res = res*10 + int64(buf[pos]-'0')
pos++
}
return res
}
n := nextInt()
if n == 0 {
return
}
cost := make([][]int64, 3)
for i := 0; i < 3; i++ {
cost[i] = make([]int64, n+1)
for j := 1; j <= n; j++ {
cost[i][j] = nextInt64()
}
}
head := make([]int, n+1)
for i := range head {
head[i] = -1
}
to := make([]int, (n-1)*2)
next := make([]int, (n-1)*2)
deg := make([]int, n+1)
edgeCnt := 0
addEdge := func(u, v int) {
to[edgeCnt] = v
next[edgeCnt] = head[u]
head[u] = edgeCnt
edgeCnt++
deg[u]++
}
for i := 0; i < n-1; i++ {
u := nextInt()
v := nextInt()
addEdge(u, v)
addEdge(v, u)
}
start := -1
for i := 1; i <= n; i++ {
if deg[i] >= 3 {
fmt.Println("-1")
return
}
if deg[i] == 1 {
start = i
}
}
path := make([]int, 0, n)
curr := start
prev := -1
for len(path) < n {
path = append(path, curr)
nxt := -1
for e := head[curr]; e != -1; e = next[e] {
v := to[e]
if v != prev {
nxt = v
break
}
}
prev = curr
curr = nxt
}
perms := [][]int{
{0, 1, 2},
{0, 2, 1},
{1, 0, 2},
{1, 2, 0},
{2, 0, 1},
{2, 1, 0},
}
minCost := int64(-1)
bestPerm := -1
for i, perm := range perms {
var currentCost int64
for j := 0; j < n; j++ {
node := path[j]
color := perm[j%3]
currentCost += cost[color][node]
}
if minCost == -1 || currentCost < minCost {
minCost = currentCost
bestPerm = i
}
}
ans := make([]int, n+1)
for j := 0; j < n; j++ {
node := path[j]
ans[node] = perms[bestPerm][j%3] + 1
}
var out bytes.Buffer
out.WriteString(strconv.FormatInt(minCost, 10))
out.WriteByte('\n')
for i := 1; i <= n; i++ {
out.WriteString(strconv.Itoa(ans[i]))
out.WriteByte(' ')
}
out.WriteByte('\n')
os.Stdout.Write(out.Bytes())
}