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.
- Define the state carried into one recursive or stack frame.
- Mark or choose the current state before exploring children.
- 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.
Use this to learn the idea, then write your own version.
1package main2 3import (4 "bufio"5 . "fmt"6 "io"7 "math/bits"8)9 1011type xorBasis02 struct{ b, d [20]int32 }12 13func (b *xorBasis02) insert(v, d int32) bool {14 for i := len(b.b) - 1; i >= 0; i-- {15 if v>>i == 0 {16 continue17 }18 if b.b[i] == 0 {19 b.b[i] = v20 b.d[i] = d21 return true22 }23 if d > b.d[i] {24 d, b.d[i] = b.d[i], d25 v, b.b[i] = b.b[i], v26 }27 v ^= b.b[i]28 }29 return false30}31 32func (b *xorBasis02) decompose(v, d int32) bool {33 for i := len(b.b) - 1; i >= 0; i-- {34 if v>>i == 0 {35 continue36 }37 if b.b[i] == 0 || b.d[i] < d {38 return false39 }40 v ^= b.b[i]41 }42 return true43}44 45func (b *xorBasis02) merge(other *xorBasis02) {46 for i := len(other.b) - 1; i >= 0; i-- {47 x := other.b[i]48 if x > 0 {49 b.insert(x, other.d[i])50 }51 }52}53 54func cf1902F(in io.Reader, _w io.Writer) {55 out := bufio.NewWriter(_w)56 defer out.Flush()57 var n, q, x, y int58 var k int3259 Fscan(in, &n)60 a := make([]int32, n)61 for i := range a {62 Fscan(in, &a[i])63 }64 g := make([][]int, n)65 for range n - 1 {66 var v, w int67 Fscan(in, &v, &w)68 v--69 w--70 g[v] = append(g[v], w)71 g[w] = append(g[w], v)72 }73 74 const mx = 1875 pa := make([][mx]int, n)76 dep := make([]int32, n)77 xb := make([]xorBasis02, n)78 var dfs func(int, int)79 dfs = func(v, p int) {80 xb[v].insert(a[v], dep[v])81 pa[v][0] = p82 for _, w := range g[v] {83 if w == p {84 continue85 }86 dep[w] = dep[v] + 187 xb[w] = xb[v]88 dfs(w, v)89 }90 }91 dfs(0, -1)92 for i := range mx - 1 {93 for v := range pa {94 p := pa[v][i]95 if p != -1 {96 pa[v][i+1] = pa[p][i]97 } else {98 pa[v][i+1] = -199 }100 }101 }102 uptoDep := func(v int, d int32) int {103 for k := uint32(dep[v] - d); k > 0; k &= k - 1 {104 v = pa[v][bits.TrailingZeros32(k)]105 }106 return v107 }108 getLCA := func(v, w int) int {109 if dep[v] > dep[w] {110 v, w = w, v111 }112 w = uptoDep(w, dep[v])113 if w == v {114 return v115 }116 for i := mx - 1; i >= 0; i-- {117 pv, pw := pa[v][i], pa[w][i]118 if pv != pw {119 v, w = pv, pw120 }121 }122 return pa[v][0]123 }124 125 Fscan(in, &q)126 for range q {127 Fscan(in, &x, &y, &k)128 x--129 y--130 b := xb[x]131 b.merge(&xb[y])132 if b.decompose(k, dep[getLCA(x, y)]) {133 Fprintln(out, "YES")134 } else {135 Fprintln(out, "NO")136 }137 }138}139 140141