- Define precisely what one DP state represents.
- Establish the base cases before transitions are evaluated.
- Process states in dependency order and combine only already-known values.
Code notes
- 81 lines of Go from the credited upstream file 1800G.go.
- The implementation visibly relies on sequence storage, cached states.
- No explicit loop blocks detected.
Complexity
Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.
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 "math/bits"8 "reflect"9 "runtime/debug"10 "sort"11 "unsafe"12)13 1415func init() { debug.SetMemoryLimit(200 << 20) }16 17func CF1800G(_r io.Reader, _w io.Writer) {18 in := bufio.NewReader(_r)19 out := bufio.NewWriter(_w)20 defer out.Flush()21 22 var T, n, v, w int23 for Fscan(in, &T); T > 0; T-- {24 Fscan(in, &n)25 g := make([][]int, n)26 for i := 1; i < n; i++ {27 Fscan(in, &v, &w)28 v--29 w--30 g[v] = append(g[v], w)31 g[w] = append(g[w], v)32 }33 tid := map[string]int{}34 sym := make([]bool, n)35 var f func(int, int) int36 f = func(v, fa int) int {37 ids := make([]int, 0, len(g[v]))38 for _, w := range g[v] {39 if w != fa {40 ids = append(ids, f(w, v))41 }42 }43 sort.Ints(ids)44 sh := (*reflect.SliceHeader)(unsafe.Pointer(&ids))45 sh.Len *= bits.UintSize / 846 s := *(*string)(unsafe.Pointer(sh))47 id, ok := tid[s]48 if !ok {49 ok := true50 cnt := map[int]int{}51 sh.Len /= bits.UintSize / 852 for _, i := range ids {53 cnt[i]++54 }55 left := len(ids) % 256 for i, c := range cnt {57 if c%2 > 0 {58 if left == 0 || !sym[i] {59 ok = false60 break61 }62 left--63 }64 }65 sh.Len *= bits.UintSize / 866 id = len(tid)67 tid[s] = id68 sym[id] = ok69 }70 return id71 }72 if sym[f(0, -1)] {73 Fprintln(out, "YES")74 } else {75 Fprintln(out, "NO")76 }77 }78}79 8081