For problem statement at 1000-1999/1900-1999/1980-1989/1980/problemG.txt this is a correct solution, but verifier at 1000-1999/1900-1999/1980-1989/1980/verifierG.go ends with case 1 failed: expected 0 got 10
input:
1
4 1
1 2 9
1 3 6
2 4 9
? 4 3
exit status 1 can you fix the verifier? ```go
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
type Edge struct {
to int
w int
}
type node struct {
child [2]int
cnt int
}
type Trie struct {
nodes []node
}
func NewTrie() *Trie {
return &Trie{
nodes: []node{{child: [2]int{-1, -1}, cnt: 0}},
}
}
func (t *Trie) Insert(x int) {
cur := 0
t.nodes[cur].cnt++
for i := 29; i >= 0; i-- {
b := (x >> i) & 1
if t.nodes[cur].child[b] == -1 {
t.nodes[cur].child[b] = len(t.nodes)
t.nodes = append(t.nodes, node{child: [2]int{-1, -1}, cnt: 0})
}
cur = t.nodes[cur].child[b]
t.nodes[cur].cnt++
}
}
func (t *Trie) Remove(x int) {
cur := 0
t.nodes[cur].cnt--
for i := 29; i >= 0; i-- {
b := (x >> i) & 1
cur = t.nodes[cur].child[b]
t.nodes[cur].cnt--
}
}
func (t *Trie) MaxXor(x int) int {
if t.nodes[0].cnt == 0 {
return -1
}
cur := 0
ans := 0
for i := 29; i >= 0; i-- {
b := (x >> i) & 1
want := 1 - b
if t.nodes[cur].child[want] != -1 && t.nodes[t.nodes[cur].child[want]].cnt > 0 {
ans |= (1 << i)
cur = t.nodes[cur].child[want]
} else {
cur = t.nodes[cur].child[b]
}
}
return ans
}
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var t int
line, _ := reader.ReadString('\n')
t, _ = strconv.Atoi(strings.TrimSpace(line))
for tc := 0; tc < t; tc++ {
line, _ = reader.ReadString('\n')
parts := strings.Fields(line)
n, _ := strconv.Atoi(parts[0])
m, _ := strconv.Atoi(parts[1])
adj := make([][]Edge, n+1)
for i := 0; i < n-1; i++ {
line, _ = reader.ReadString('\n')
parts = strings.Fields(line)
v, _ := strconv.Atoi(parts[0])
u, _ := strconv.Atoi(parts[1])
w, _ := strconv.Atoi(parts[2])
adj[v] = append(adj[v], Edge{to: u, w: w})
adj[u] = append(adj[u], Edge{to: v, w: w})
}
prefix := make([]int, n+1)
depth := make([]int, n+1)
parent := make([]int, n+1)
parent[1] = -1
queue := []int{1}
parent[1] = 0
for len(queue) > 0 {
v := queue[0]
queue = queue[1:]
for _, e := range adj[v] {
if e.to != parent[v] {
parent[e.to] = v
depth[e.to] = depth[v] + 1
prefix[e.to] = prefix[v] ^ e.w
queue = append(queue, e.to)
}
}
}
trieEven := NewTrie()
trieOdd := NewTrie()
for u := 1; u <= n; u++ {
if depth[u]%2 == 0 {
trieEven.Insert(prefix[u])
} else {
trieOdd.Insert(prefix[u])
}
}
add := 0
for i := 0; i < m; i++ {
line, err := reader.ReadString('\n')
if err != nil && err != io.EOF {
break
}
line = strings.TrimSpace(line)
parts = strings.Fields(line)
if parts[0] == "^" {
y, _ := strconv.Atoi(parts[1])
add ^= y
} else {
v, _ := strconv.Atoi(parts[1])
x, _ := strconv.Atoi(parts[2])
base := prefix[v] ^ x
var ans int
if depth[v]%2 == 0 {
trieEven.Remove(prefix[v])
ans1 := trieEven.MaxXor(base)
trieEven.Insert(prefix[v])
ans2 := trieOdd.MaxXor(base ^ add)
ans = ans1
if ans2 > ans {
ans = ans2
}
} else {
ans1 := trieEven.MaxXor(base ^ add)
trieOdd.Remove(prefix[v])
ans2 := trieOdd.MaxXor(base)
trieOdd.Insert(prefix[v])
ans = ans1
if ans2 > ans {
ans = ans2
}
}
fmt.Fprintln(writer, ans)
}
}
}
}
```