Approach
Depth-first search
For Codeforces 1491E — Fib-tree, 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
- 71 lines of Go from the credited upstream file 1491E.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 "sort"8)9 1011func cf1491E(_r io.Reader, out io.Writer) {12 in := bufio.NewReader(_r)13 var n, v, w int14 Fscan(in, &n)15 f := []int{1, 1}16 for f[len(f)-1]+f[len(f)-2] <= n {17 f = append(f, f[len(f)-1]+f[len(f)-2])18 }19 if f[len(f)-1] != n {20 Fprint(out, "NO")21 return22 }23 24 type edge struct{ to, eid int }25 g := make([][]edge, n+1)26 for i := 0; i < n-1; i++ {27 Fscan(in, &v, &w)28 g[v] = append(g[v], edge{w, i})29 g[w] = append(g[w], edge{v, i})30 }31 32 vis := make([]bool, n-1)33 var check func(int, int) bool34 check = func(rt, tar int) bool {35 if tar == 1 {36 return true37 }38 t := f[sort.SearchInts(f, tar)-1]39 var x, y, z int40 var dfs func(int, int) int41 dfs = func(v, fa int) int {42 size := 143 for _, e := range g[v] {44 w := e.to45 if w == fa || vis[e.eid] {46 continue47 }48 sz := dfs(w, v)49 if z > 0 {50 return 051 }52 if sz == t || sz == tar-t {53 vis[e.eid] = true54 x, y, z = w, v, sz55 }56 size += sz57 }58 return size59 }60 dfs(rt, 0)61 return z > 0 && check(x, z) && check(y, tar-z)62 }63 if check(1, n) {64 Fprint(out, "YES")65 } else {66 Fprint(out, "NO")67 }68}69 7071