- 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
- 96 lines of Go from the credited upstream file 1239D.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 CF1239D(_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 T, n, m, v, w int22 for Fscan(in, &T); T > 0; T-- {23 Fscan(in, &n, &m)24 g := make([][]int, n)25 for ; m > 0; m-- {26 Fscan(in, &v, &w)27 if v != w {28 g[v-1] = append(g[v-1], w-1)29 }30 }31 sccID := make([]int, n)32 sid := 033 c0 := 034 dfn := make([]int, n)35 dfsClock := 036 s := []int{}37 inS := make([]bool, n)38 var f func(int) int39 f = func(v int) int {40 dfsClock++41 dfn[v] = dfsClock42 lowV := dfsClock43 s = append(s, v)44 inS[v] = true45 for _, w := range g[v] {46 if dfn[w] == 0 {47 lowV = min(lowV, f(w))48 } else if inS[w] {49 lowV = min(lowV, dfn[w])50 }51 }52 if dfn[v] == lowV {53 for {54 w := s[len(s)-1]55 s = s[:len(s)-1]56 inS[w] = false57 sccID[w] = sid58 if sid == 0 {59 c0++60 }61 if w == v {62 break63 }64 }65 sid++66 }67 return lowV68 }69 for v, ts := range dfn {70 if ts == 0 {71 f(v)72 }73 }74 if sid == 1 {75 Fprintln(out, "No")76 continue77 }78 Fprintln(out, "Yes")79 Fprintln(out, c0, n-c0)80 for i, id := range sccID {81 if id == 0 {82 Fprint(out, i+1, " ")83 }84 }85 Fprintln(out)86 for i, id := range sccID {87 if id > 0 {88 Fprint(out, i+1, " ")89 }90 }91 Fprintln(out)92 }93}94 9596