Approach
Depth-first search
For Codeforces 1088E — Ehab and a component choosing problem, 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
- 59 lines of Go from the credited upstream file 1088E.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 . "fmt"5 "io"6)7 89func cf1088E(in io.Reader, out io.Writer) {10 var n, ans int11 Fscan(in, &n)12 a := make([]int, n)13 for i := range a {14 Fscan(in, &a[i])15 }16 g := make([][]int, n)17 for range n - 1 {18 var v, w int19 Fscan(in, &v, &w)20 v--21 w--22 g[v] = append(g[v], w)23 g[w] = append(g[w], v)24 }25 26 mx := int(-1e18)27 var dfs func(int, int) int28 dfs = func(v, fa int) int {29 res := a[v]30 for _, w := range g[v] {31 if w != fa {32 res += dfs(w, v)33 }34 }35 mx = max(mx, res)36 return max(res, 0)37 }38 dfs(0, -1)39 40 dfs = func(v, fa int) int {41 res := a[v]42 for _, w := range g[v] {43 if w != fa {44 res += dfs(w, v)45 }46 }47 if res == mx {48 ans++49 return 050 }51 return max(res, 0)52 }53 dfs(0, -1)54 55 Fprint(out, mx*ans, ans)56}57 5859