Graph Theory37 sections · 1633 units
Open in Course

Space Improvement Note

(Reducing memory usage)

If nn is huge (say, 10610^6), the O(nlog⁡n)O(n \log n) space might be tight. You can improve by only storing up[v][j] for jj up to the depth of vv. For example, if vv has depth 55, you only need up[v][0] through up[v][2] (since 23=8>52^3 = 8 > 5). This saves space in shallow trees.

But for most problems, the standard O(nlog⁡n)O(n \log n) space is fine. Only optimize when the memory limit is tight and the tree is known to be shallow.