Problem solution · Go

Codeforces 1902F — Trees and XOR Queries Again

Codeforces 1902F — Trees and XOR Queries Again: a Go solution using depth-first search. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Depth-first search
Source
EndlessCheng Codeforces Go
Length
141 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Depth-first search

For Codeforces 1902F — Trees and XOR Queries Again, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.

  1. Define the state carried into one recursive or stack frame.
  2. Mark or choose the current state before exploring children.
  3. Combine child results or undo the choice when the branch finishes.

Code notes

  • 141 lines of Go from the credited upstream file 1902F.go.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected, together with recursive traversal.

Complexity

Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from EndlessCheng Codeforces Go by Σndless (EndlessCheng) and is used under the MIT licence.

Full codeCodeforces 1902F — Trees and XOR Queries Again · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io"	"math/bits") // https://github.com/EndlessChengtype xorBasis02 struct{ b, d [20]int32 } func (b *xorBasis02) insert(v, d int32) bool {	for i := len(b.b) - 1; i >= 0; i-- {		if v>>i == 0 {			continue		}		if b.b[i] == 0 {			b.b[i] = v			b.d[i] = d			return true		}		if d > b.d[i] {			d, b.d[i] = b.d[i], d			v, b.b[i] = b.b[i], v		}		v ^= b.b[i]	}	return false} func (b *xorBasis02) decompose(v, d int32) bool {	for i := len(b.b) - 1; i >= 0; i-- {		if v>>i == 0 {			continue		}		if b.b[i] == 0 || b.d[i] < d {			return false		}		v ^= b.b[i]	}	return true} func (b *xorBasis02) merge(other *xorBasis02) {	for i := len(other.b) - 1; i >= 0; i-- {		x := other.b[i]		if x > 0 {			b.insert(x, other.d[i])		}	}} func cf1902F(in io.Reader, _w io.Writer) {	out := bufio.NewWriter(_w)	defer out.Flush()	var n, q, x, y int	var k int32	Fscan(in, &n)	a := make([]int32, n)	for i := range a {		Fscan(in, &a[i])	}	g := make([][]int, n)	for range n - 1 {		var v, w int		Fscan(in, &v, &w)		v--		w--		g[v] = append(g[v], w)		g[w] = append(g[w], v)	} 	const mx = 18	pa := make([][mx]int, n)	dep := make([]int32, n)	xb := make([]xorBasis02, n)	var dfs func(int, int)	dfs = func(v, p int) {		xb[v].insert(a[v], dep[v])		pa[v][0] = p		for _, w := range g[v] {			if w == p {				continue			}			dep[w] = dep[v] + 1			xb[w] = xb[v]			dfs(w, v)		}	}	dfs(0, -1)	for i := range mx - 1 {		for v := range pa {			p := pa[v][i]			if p != -1 {				pa[v][i+1] = pa[p][i]			} else {				pa[v][i+1] = -1			}		}	}	uptoDep := func(v int, d int32) int {		for k := uint32(dep[v] - d); k > 0; k &= k - 1 {			v = pa[v][bits.TrailingZeros32(k)]		}		return v	}	getLCA := func(v, w int) int {		if dep[v] > dep[w] {			v, w = w, v		}		w = uptoDep(w, dep[v])		if w == v {			return v		}		for i := mx - 1; i >= 0; i-- {			pv, pw := pa[v][i], pa[w][i]			if pv != pw {				v, w = pv, pw			}		}		return pa[v][0]	} 	Fscan(in, &q)	for range q {		Fscan(in, &x, &y, &k)		x--		y--		b := xb[x]		b.merge(&xb[y])		if b.decompose(k, dep[getLCA(x, y)]) {			Fprintln(out, "YES")		} else {			Fprintln(out, "NO")		}	}} //func main() { cf1902F(bufio.NewReader(os.Stdin), os.Stdout) } 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗