- 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
- 100 lines of Go from the credited upstream file 962F.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 CF962F(_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 nei struct{ to, eid int }24 g := make([][]nei, n)25 for i := 1; i <= m; i++ {26 Fscan(in, &v, &w)27 v--28 w--29 g[v] = append(g[v], nei{w, i})30 g[w] = append(g[w], nei{v, i})31 }32 33 onSimpleCycle := make([]bool, m+1)34 bccIDs := make([]int, n)35 idCnt := 036 dfn := make([]int, n)37 dfsClock := 038 type edge struct{ v, w, eid int }39 stack := []edge{}40 var f func(v, fa int) int41 f = func(v, fa int) int {42 dfsClock++43 dfn[v] = dfsClock44 lowV := dfsClock45 for _, ne := range g[v] {46 w := ne.to47 e := edge{v, w, ne.eid}48 if dfn[w] == 0 {49 stack = append(stack, e)50 lowW := f(w, v)51 if lowW >= dfn[v] {52 idCnt++53 var comp, eids []int54 for {55 e, stack = stack[len(stack)-1], stack[:len(stack)-1]56 if bccIDs[e.v] != idCnt {57 bccIDs[e.v] = idCnt58 comp = append(comp, e.v)59 }60 if bccIDs[e.w] != idCnt {61 bccIDs[e.w] = idCnt62 comp = append(comp, e.w)63 }64 eids = append(eids, e.eid)65 if e.v == v && e.w == w {66 break67 }68 }69 if len(comp) == len(eids) {70 for _, eid := range eids {71 onSimpleCycle[eid] = true72 }73 }74 }75 lowV = min(lowV, lowW)76 } else if w != fa && dfn[w] < dfn[v] {77 stack = append(stack, e)78 lowV = min(lowV, dfn[w])79 }80 }81 return lowV82 }83 for v, ts := range dfn {84 if ts == 0 && len(g[v]) > 0 {85 f(v, -1)86 }87 }88 89 ans := []interface{}{}90 for i, b := range onSimpleCycle {91 if b {92 ans = append(ans, i)93 }94 }95 Fprintln(out, len(ans))96 Fprint(out, ans...)97}98 99100