- 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
- 55 lines of Go from the credited upstream file 553C.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 CF553C(_r io.Reader, out io.Writer) {11 in := bufio.NewReader(_r)12 const mod int = 1e9 + 713 14 var n, m, v, w, t int15 Fscan(in, &n, &m)16 g := [2][][]int{make([][]int, n), make([][]int, n)}17 for ; m > 0; m-- {18 Fscan(in, &v, &w, &t)19 v--20 w--21 g[t][v] = append(g[t][v], w)22 g[t][w] = append(g[t][w], v)23 }24 25 colors := make([]int8, n)26 var f func(int, int8) bool27 f = func(v int, c int8) bool {28 colors[v] = c29 for _, w := range g[1][v] {30 if colors[w] == -c || colors[w] == 0 && !f(w, c) {31 return false32 }33 }34 for _, w := range g[0][v] {35 if colors[w] == c || colors[w] == 0 && !f(w, -c) {36 return false37 }38 }39 return true40 }41 ans := (mod + 1) / 242 for i, c := range colors {43 if c == 0 {44 if !f(i, 1) {45 Fprint(out, 0)46 return47 }48 ans = ans * 2 % mod49 }50 }51 Fprint(out, ans)52}53 5455