- 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
- 99 lines of Go from the credited upstream file 1131D.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 Sol1131D(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 fa := make([]int, n+m)17 for i := range fa {18 fa[i] = i19 }20 var find func(int) int21 find = func(x int) int {22 if fa[x] != x {23 fa[x] = find(fa[x])24 }25 return fa[x]26 }27 merge := func(from, to int) { fa[find(from)] = find(to) }28 29 cp := make([]string, n)30 for i := range cp {31 Fscan(in, &cp[i])32 for j, c := range cp[i] {33 if c == '=' {34 merge(n+j, i)35 }36 }37 }38 for i := range fa {39 find(i)40 }41 42 g := make([][]int, n+m)43 inDeg := make([]int, n+m)44 for i, s := range cp {45 for j, c := range s {46 if c == '=' {47 continue48 }49 v, w := fa[i], fa[n+j]50 if v == w {51 Fprint(out, "No")52 return53 }54 if c == '>' {55 v, w = w, v56 }57 g[v] = append(g[v], w)58 inDeg[w]++59 }60 }61 queue := []int{}62 ans := make([]int, n+m)63 cnt := 064 for i, deg := range inDeg {65 if deg == 0 {66 queue = append(queue, i)67 ans[i] = 168 cnt++69 }70 }71 for len(queue) > 0 {72 var v int73 v, queue = queue[0], queue[1:]74 for _, w := range g[v] {75 inDeg[w]--76 if inDeg[w] == 0 {77 queue = append(queue, w)78 ans[w] = ans[v] + 179 cnt++80 }81 }82 }83 if cnt < n+m {84 Fprint(out, "No")85 return86 }87 Fprintln(out, "Yes")88 for i, o := range fa {89 if i == n {90 Fprintln(out)91 }92 Fprint(out, ans[o], " ")93 }94}95 96979899