- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 69 lines of Go from the credited upstream file 1067B.go.
- The implementation visibly relies on sequence storage.
- 1 loop block detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
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 CF1067B(_r io.Reader, _w io.Writer) (ans bool) {11 in := bufio.NewReader(_r)12 defer func() {13 if ans {14 Fprint(_w, "Yes")15 } else {16 Fprint(_w, "No")17 }18 }()19 20 var n, k, v, w int21 Fscan(in, &n, &k)22 if n < 4 {23 return false24 }25 g := make([][]int, n+1)26 for i := 1; i < n; i++ {27 Fscan(in, &v, &w)28 g[v] = append(g[v], w)29 g[w] = append(g[w], v)30 }31 32 vis := make([]bool, n+1)33 centers := map[int]int{}34 for i, gi := range g {35 if len(gi) == 1 {36 vis[i] = true37 centers[gi[0]]++38 }39 }40 for {41 k--42 newCenters := map[int]int{}43 for v, cnt1 := range centers {44 if cnt1 < 3 {45 return false46 }47 ct := 048 for _, w := range g[v] {49 if !vis[w] {50 if ct != 0 || centers[w] > 0 {51 return false52 }53 ct = w54 }55 }56 if ct == 0 {57 return k == 058 }59 vis[v] = true60 newCenters[ct]++61 }62 centers = newCenters63 }64}65 66676869