Approach
Depth-first search
For Codeforces 1923E — Count Paths, 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
- 52 lines of Go from the credited upstream file 1923E.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 cf1923E(_r io.Reader, _w io.Writer) {11 in := bufio.NewReader(_r)12 out := bufio.NewWriter(_w)13 defer out.Flush()14 15 var T, n, v, w int16 for Fscan(in, &T); T > 0; T-- {17 Fscan(in, &n)18 cs := make([]int, n)19 for i := range cs {20 Fscan(in, &cs[i])21 }22 g := make([][]int, n)23 for i := 1; i < n; i++ {24 Fscan(in, &v, &w)25 v--26 w--27 g[v] = append(g[v], w)28 g[w] = append(g[w], v)29 }30 31 ans := 032 cnt := make([]int, n+1)33 var dfs func(int, int)34 dfs = func(v, fa int) {35 c := cs[v]36 tmp := cnt[c]37 ans += tmp38 for _, w := range g[v] {39 if w != fa {40 cnt[c] = 141 dfs(w, v)42 }43 }44 cnt[c] = tmp + 145 }46 dfs(0, -1)47 Fprintln(out, ans)48 }49}50 5152