- 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
- 61 lines of Go from the credited upstream file 1650G.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 . "fmt"5 "io"6)7 8func cf1650G(in io.Reader, out io.Writer) {9 const mod = 1_000_000_00710 var T, n, m, s, t int11 for Fscan(in, &T); T > 0; T-- {12 Fscan(in, &n, &m, &s, &t)13 s--14 t--15 g := make([][]int, n)16 for ; m > 0; m-- {17 var v, w int18 Fscan(in, &v, &w)19 v--20 w--21 g[v] = append(g[v], w)22 g[w] = append(g[w], v)23 }24 25 26 f := make([]int, n*2)27 dis := make([]int, n*2)28 for i := range dis {29 dis[i] = -130 }31 dis[s*2] = 032 f[s*2] = 133 q := []int{s * 2}34 for len(q) > 0 {35 v := q[0]36 q = q[1:]37 newD := dis[v] + 138 for _, w := range g[v/2] {39 w *= 240 if dis[w] < 0 {41 dis[w] = newD42 f[w] = f[v]43 q = append(q, w)44 } else if newD == dis[w] {45 f[w] = (f[w] + f[v]) % mod46 } else if newD == dis[w]+1 { 47 w++48 if dis[w] < 0 {49 dis[w] = newD50 q = append(q, w)51 }52 f[w] = (f[w] + f[v]) % mod53 }54 }55 }56 Fprintln(out, (f[t*2]+f[t*2+1])%mod)57 }58}59 6061