- 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
- 83 lines of Go from the credited upstream file 1811F.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks 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 "math"8)9 1011func CF1811F(_r io.Reader, _w io.Writer) {12 in := bufio.NewReader(_r)13 out := bufio.NewWriter(_w)14 defer out.Flush()15 16 f := func() bool {17 var n, m, v, w, c4, cur int18 Fscan(in, &n, &m)19 g := make([][]int, n)20 for i := 0; i < m; i++ {21 Fscan(in, &v, &w)22 v--23 w--24 g[v] = append(g[v], w)25 g[w] = append(g[w], v)26 }27 k := int(math.Sqrt(float64(n)))28 if k < 3 || k*k != n || k*(k+1) != m {29 return false30 }31 for i, ws := range g {32 if len(ws) == 4 {33 c4++34 cur = i35 } else if len(ws) != 2 {36 return false37 }38 }39 if c4 != k {40 return false41 }42 vis := make([]bool, n)43 for !vis[cur] {44 vis[cur] = true45 for _, w := range g[cur] {46 if vis[w] {47 continue48 }49 if len(g[w]) == 4 {50 cur = w51 continue52 }53 cnt := 154 for !vis[w] {55 vis[w] = true56 cnt++57 for _, w2 := range g[w] {58 if !vis[w2] {59 w = w260 break61 }62 }63 }64 if cnt != k {65 return false66 }67 c4--68 }69 }70 return c4 == 071 }72 var T int73 for Fscan(in, &T); T > 0; T-- {74 if f() {75 Fprintln(out, "YES")76 } else {77 Fprintln(out, "NO")78 }79 }80}81 8283