- 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
- 77 lines of Go from the credited upstream file 1263F.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)8 910func CF1263F(_r io.Reader, out io.Writer) {11 in := bufio.NewReader(_r)12 min := func(a, b int) int {13 if a < b {14 return a15 }16 return b17 }18 max := func(a, b int) int {19 if a > b {20 return a21 }22 return b23 }24 25 var n, m, v int26 Fscan(in, &n)27 maxDel := make([][]int, n+1)28 for i := range maxDel {29 maxDel[i] = make([]int, n+1)30 }31 f := func() {32 Fscan(in, &m)33 g := make([][]int, m+1)34 for w := 2; w <= m; w++ {35 Fscan(in, &v)36 g[v] = append(g[v], w)37 }38 tar := make([]int, m+1)39 for i := 1; i <= n; i++ {40 Fscan(in, &v)41 tar[v] = i42 }43 var f func(int) (sz, l, r int)44 f = func(v int) (edgeSize, l, r int) {45 if v > 1 {46 edgeSize = 1 47 }48 l = 1e949 if tar[v] > 0 {50 l, r = tar[v], tar[v]51 }52 for _, w := range g[v] {53 sz, ll, rr := f(w)54 edgeSize += sz55 l = min(l, ll)56 r = max(r, rr)57 }58 59 maxDel[l][r] = max(maxDel[l][r], edgeSize)60 return61 }62 f(1)63 }64 f()65 f()66 67 dp := make([]int, n+1)68 for i := range dp {69 for j, dv := range dp[:i] {70 dp[i] = max(dp[i], dv+maxDel[j+1][i])71 }72 }73 Fprint(out, dp[n])74}75 7677