- 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
- 74 lines of Go from the credited upstream file 936B.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)8 910func CF936B(_r io.Reader, _w io.Writer) {11 in := bufio.NewReader(_r)12 out := bufio.NewWriter(_w)13 defer out.Flush()14 15 var n, m, sz, st int16 Fscan(in, &n, &m)17 g := make([][]int, n)18 for i := range g {19 Fscan(in, &sz)20 g[i] = make([]int, sz)21 for j := range g[i] {22 Fscan(in, &g[i][j])23 g[i][j]--24 }25 }26 Fscan(in, &st)27 st--28 29 path := []int{}30 vis := make([][2]bool, n)31 var f func(int, int8) bool32 f = func(v int, step int8) bool {33 path = append(path, v)34 if step > 0 && len(g[v]) == 0 {35 return true36 }37 vis[v][step] = true38 for _, w := range g[v] {39 if !vis[w][step^1] && f(w, step^1) {40 return true41 }42 }43 path = path[:len(path)-1]44 return false45 }46 if f(st, 0) {47 Fprintln(out, "Win")48 for _, v := range path {49 Fprint(out, v+1, " ")50 }51 return52 }53 54 vis2 := make([]int8, n)55 var f2 func(int) bool56 f2 = func(v int) bool {57 vis2[v] = 158 for _, w := range g[v] {59 if tp := vis2[w]; tp == 1 || tp == 0 && f2(w) {60 return true61 }62 }63 vis2[v] = 264 return false65 }66 if f2(st) {67 Fprint(out, "Draw")68 } else {69 Fprint(out, "Lose")70 }71}72 7374