Constructor:
Store and parent array.
Compute .
Build up[n][LOG]:
up[i][0] = parent[i]
up[i][j] = up[up[i][j-1]][j-1]
:
For to , if 1, set node = up[node][j].
If node becomes , return .
Return node.
This runs in time and uses space.
(Class-based binary lifting)
Constructor:
Store and parent array.
Compute .
Build up[n][LOG]:
up[i][0] = parent[i]
up[i][j] = up[up[i][j-1]][j-1]
:
For to , if 1, set node = up[node][j].
If node becomes , return .
Return node.
This runs in time and uses space.