Approach
Depth-first search
For Codeforces 1779F — Xorcerer's Stones, 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
- 99 lines of Go from the credited upstream file 1779F.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 1011func cf1779F(in io.Reader, _w io.Writer) {12 out := bufio.NewWriter(_w)13 defer out.Flush()14 var n, p int15 Fscan(in, &n)16 a := make([]int, n)17 for i := range a {18 Fscan(in, &a[i])19 }20 g := make([][]int, n)21 for w := 1; w < n; w++ {22 Fscan(in, &p)23 p--24 g[p] = append(g[p], w)25 }26 27 sz := make([]int, n)28 preF := make([]int, n)29 fs := make([]int, n)30 var dfs0 func(int)31 dfs0 = func(v int) {32 sz[v] = 133 f := 134 for _, w := range g[v] {35 preF[w] = f36 dfs0(w)37 sz[v] += sz[w]38 nf := 039 for s := uint32(fs[w]); s > 0; s &= s - 1 {40 p := bits.TrailingZeros32(s)41 for t := uint32(f); t > 0; t &= t - 1 {42 q := bits.TrailingZeros32(t)43 nf |= 1 << (p ^ q)44 }45 }46 f = nf47 }48 49 nf := 050 for s := uint32(f); s > 0; s &= s - 1 {51 p := bits.TrailingZeros32(s)52 nf |= 1 << (p ^ a[v])53 }54 f = nf55 56 if sz[v]%2 == 0 {57 f |= 158 }59 60 fs[v] = f61 }62 dfs0(0)63 64 if fs[0]&1 == 0 {65 Fprint(out, -1)66 return67 }68 69 ans := []int{}70 var dfs func(int, int)71 dfs = func(v, tar int) {72 if tar == 0 && sz[v]%2 == 0 {73 ans = append(ans, v)74 return75 }76 tar ^= a[v]77 for i := len(g[v]) - 1; i >= 0; i-- {78 w := g[v][i]79 for s := uint32(fs[w]); s > 0; s &= s - 1 {80 p := bits.TrailingZeros32(s)81 if preF[w]>>(tar^p)&1 > 0 {82 tar ^= p83 dfs(w, p)84 break85 }86 }87 }88 }89 dfs(0, 0)90 91 ans = append(ans, 0)92 Fprintln(out, len(ans))93 for _, v := range ans {94 Fprint(out, v+1, " ")95 }96}97 9899