- 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
- 71 lines of Go from the credited upstream file 118E.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 CF118E(_r io.Reader, _w io.Writer) {11 in := bufio.NewReader(_r)12 out := bufio.NewWriter(_w)13 defer out.Flush()14 min := func(a, b int) int {15 if a < b {16 return a17 }18 return b19 }20 21 var n, m, v, w int22 Fscan(in, &n, &m)23 type nb struct{ to, i int }24 g := make([][]nb, n+1)25 for i := 0; i < m; i++ {26 Fscan(in, &v, &w)27 g[v] = append(g[v], nb{w, i})28 g[w] = append(g[w], nb{v, i})29 }30 dfn := make([]int, n+1)31 ts := 032 var f func(int, int) int33 f = func(v, fa int) int {34 ts++35 dfn[v] = ts36 lowV := ts37 for _, e := range g[v] {38 if w := e.to; dfn[w] == 0 {39 lowW := f(w, v)40 if lowW > dfn[v] {41 return 1e942 }43 lowV = min(lowV, lowW)44 } else if w != fa {45 lowV = min(lowV, dfn[w])46 }47 }48 return lowV49 }50 if f(1, 0) == 1e9 {51 Fprint(out, 0)52 return53 }54 vis := make([]bool, m)55 var f2 func(int)56 f2 = func(v int) {57 for len(g[v]) > 0 {58 e := g[v][0]59 g[v] = g[v][1:]60 if !vis[e.i] {61 vis[e.i] = true62 Fprintln(out, v, e.to)63 f2(e.to)64 }65 }66 }67 f2(1)68}69 7071