- 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
- 67 lines of Go from the credited upstream file 1230C.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 9func Sol1230C(reader io.Reader, writer io.Writer) {10 in := bufio.NewReader(reader)11 out := bufio.NewWriter(writer)12 defer out.Flush()13 14 var n, m int15 Fscan(in, &n, &m)16 edges := make([][]int, n+1)17 shown := map[int]int{}18 for i := 0; i < m; i++ {19 var v, w int20 Fscan(in, &v, &w)21 edges[v] = append(edges[v], w)22 edges[w] = append(edges[w], v)23 shown[v] = 124 shown[w] = 125 }26 if len(shown) <= 6 {27 Fprintln(out, m)28 return29 }30 31 ans := 032 for v, e := range edges {33 if len(e) == 0 {34 continue35 }36 37 for w, e2 := range edges {38 if w == v || len(e2) == 0 {39 continue40 }41 delta := 042 for _, u := range e2 {43 if u == v {44 delta = 145 break46 }47 }48 mp := map[int]int{}49 for _, u := range append(e, e2...) {50 if u == v {51 u = w52 }53 mp[u] = 154 }55 delta = len(e) + len(e2) - delta - len(mp)56 if newAns := m - delta; newAns > ans {57 ans = newAns58 }59 }60 }61 Fprintln(out, ans)62}63 64656667