Approach
Depth-first search
For Codeforces 1805D — A Wide, Wide Graph, 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
- 67 lines of Go from the credited upstream file 1805D.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 "slices"8)9 1011func cf1805D(in io.Reader, _w io.Writer) {12 out := bufio.NewWriter(_w)13 defer out.Flush()14 var n int15 Fscan(in, &n)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 tmp := make([]int, n)27 maxD, u := -1, 028 var dfs func(int, int, int)29 dfs = func(v, fa, d int) {30 tmp[v] = d31 if d > maxD {32 maxD, u = d, v33 }34 for _, w := range g[v] {35 if w != fa {36 dfs(w, v, d+1)37 }38 }39 }40 41 dfs(0, -1, 0)42 43 maxD = -144 dfs(u, -1, 0)45 d1 := slices.Clone(tmp)46 47 maxD = -148 dfs(u, -1, 0)49 d2 := tmp50 51 s := make([]int, n+1)52 for i, d := range d1 {53 if i != u { 54 55 s[max(d, d2[i])+1]++ 56 }57 }58 59 sum := 160 for i := 1; i <= n; i++ {61 sum += s[i]62 Fprint(out, sum, " ")63 }64}65 6667