Approach
Depth-first search
For Codeforces 95E — Lucky Country, 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
- 82 lines of Go from the credited upstream file 95E.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 CF95E(_r io.Reader, out io.Writer) {11 in := bufio.NewReader(_r)12 min := func(a, b int) int {13 if a > b {14 return b15 }16 return a17 }18 19 var n, m, v, w int20 Fscan(in, &n, &m)21 g := make([][]int, n)22 for ; m > 0; m-- {23 Fscan(in, &v, &w)24 v--25 w--26 g[v] = append(g[v], w)27 g[w] = append(g[w], v)28 }29 30 vis := make([]bool, n)31 var dfs func(int) int32 dfs = func(v int) int {33 vis[v] = true34 sz := 135 for _, w := range g[v] {36 if !vis[w] {37 sz += dfs(w)38 }39 }40 return sz41 }42 cnt := map[int]int{}43 for i, b := range vis {44 if !b {45 cnt[dfs(i)]++46 }47 }48 49 f := make([]int, n+1)50 for i := 1; i <= n; i++ {51 f[i] = 1e952 }53 maxJ := 054 for w, num := range cnt {55 maxJ += w * num56 for k1 := 1; num > 0; k1 <<= 1 {57 k := min(k1, num)58 for j := maxJ; j >= k*w; j-- {59 f[j] = min(f[j], f[j-k*w]+k)60 }61 num -= k62 }63 }64 ans := int(1e9)65o:66 for i := 1; i <= n; i++ {67 for x := i; x > 0; x /= 10 {68 if x%10 != 4 && x%10 != 7 {69 continue o70 }71 }72 ans = min(ans, f[i])73 }74 if ans == 1e9 {75 Fprint(out, -1)76 } else {77 Fprint(out, ans-1)78 }79}80 8182