Approach
Depth-first search
For Codeforces 1031B — Curiosity Has No Limits, 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
- 56 lines of Go from the credited upstream file 1031B.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)8 910func cf1031B(in io.Reader, _w io.Writer) {11 out := bufio.NewWriter(_w)12 defer out.Flush()13 var n int14 Fscan(in, &n)15 a := make([]int, n-1)16 for i := range a {17 Fscan(in, &a[i])18 }19 b := make([]int, n-1)20 for i := range b {21 Fscan(in, &b[i])22 }23 24 const mx = 425 ans := make([]any, n)26 vis := make([][mx]bool, n)27 var dfs func(int, int) bool28 dfs = func(i, j int) bool {29 if i < 0 {30 return true31 }32 if vis[i][j] {33 return false34 }35 vis[i][j] = true36 for k := range mx {37 if k|j == a[i] && k&j == b[i] && dfs(i-1, k) {38 ans[i] = k39 return true40 }41 }42 return false43 }44 for j := range mx {45 ans[n-1] = j46 if dfs(n-2, j) {47 Fprintln(out, "YES")48 Fprintln(out, ans...)49 return50 }51 }52 Fprint(out, "NO")53}54 5556