Graph Theory37 sections · 1633 units
Open in Course

Prefix-Suffix Arrays (Excluding one child)

(Efficient sibling exclusion)

When computing up[v] for child vv of node uu, you need the combined contribution of all other children of uu, excluding vv. If uu has kk children, recomputing the combination for each child takes O(k)O(k) per child and O(k2)O(k^2) total.

The fix: build prefix and suffix arrays over uu's children. prefix[i] combines children c1c_1 through cic_i. suffix[i] combines children cic_i through ckc_k. To exclude child cic_i, combine prefix[i-1] with suffix[i+1] in O(1)O(1). This keeps the total work per node proportional to its degree, giving O(n)O(n) overall.