- 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
- 87 lines of Go from the credited upstream file 1635E.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 CF1635E(_r io.Reader, _w io.Writer) {11 in := bufio.NewReader(_r)12 out := bufio.NewWriter(_w)13 defer out.Flush()14 15 var n, m int16 Fscan(in, &n, &m)17 es := make([][3]int, m)18 g := make([][]int, n)19 for i := range es {20 Fscan(in, &es[i][0], &es[i][1], &es[i][2])21 v, w := es[i][1]-1, es[i][2]-122 g[v] = append(g[v], w)23 g[w] = append(g[w], v)24 }25 26 col := make([]int8, n)27 var f func(int, int8) bool28 f = func(v int, c int8) bool {29 col[v] = c30 for _, w := range g[v] {31 if col[w] == c || col[w] == 0 && !f(w, 3^c) {32 return false33 }34 }35 return true36 }37 for i, c := range col {38 if c == 0 && !f(i, 1) {39 Fprint(out, "NO")40 return41 }42 }43 44 g = make([][]int, n)45 d := make([]int, n)46 for _, e := range es {47 v, w := e[1]-1, e[2]-148 if col[v] == 2 {49 v, w = w, v50 }51 if e[0] == 1 {52 g[v] = append(g[v], w)53 d[w]++54 } else {55 g[w] = append(g[w], v)56 d[v]++57 }58 }59 q := make([]int, 0, n)60 for i, d := range d {61 if d == 0 {62 q = append(q, i)63 }64 }65 ans := make([]int, n)66 for len(q) > 0 {67 v := q[0]68 q = q[1:]69 ans[v] = cap(q)70 for _, w := range g[v] {71 if d[w]--; d[w] == 0 {72 q = append(q, w)73 }74 }75 }76 if cap(q) > 0 {77 Fprint(out, "NO")78 return79 }80 Fprintln(out, "YES")81 for i, v := range ans {82 Fprintf(out, "%c %d\n", " RL"[col[i]], v)83 }84}85 8687