Link Cut Tree
Data structure that extends lazy segment tree for dynamic trees with vertices of monoid to support:
- link: given two vertices
uandv, makeu's parentv. - cut: remove the edge between a node and its parent.
- evert: make a node the root of the tree.
- connectivity detection: determine whether two vertices are in the same connected component
- range update: update all vertices on the path between two vertices with a function (assuming these vertices are connected)
- range query: find the product of all vertices on the path between two vertices (assuming these vertices are connected)
__init__
Arguments
A: list[T]: array of monoiddot: Callable[[T, T], T]: binary operator corresponding toe: T: identity element ofcompose: Callable[[U, U], U]: binary operator corresponding to inid: U: identity element ofact: Callable[[U, T, int], T]: binary operator corresponding to- The third argument represents the size (length) of the path being updated.
- This supports aggregations dependent on component size, such as range add/range sum (conceptually extending to , which is called interval extension).
Complexities
- time:
- space:
where represents the length of A.
evert
Make a given node v the root of the tree including v.
Arguments
v: int: node to make the root of the tree
Complexities
lca
Returns the lowest common ancestor of two given nodes u and v.
Note: Ancestors are dependant on the root, so you must explicitly call evert before using this method.
Arguments
u: int: first nodev: int: second node
Returns
int: lowest common ancestor ofuandv
Complexities
link
Given two nodes u and v, make u's parent v.
Arguments
u: int: the child node ofvv: int: the parent node ofu
Complexities
cut
Given a node u, remove the edge between u and its parent.
Arguments
u: int: node to remove the edge from
Complexities
is_connected
Returns whether vertices u and v are connected.
Arguments
u: int: vertexv: int: vertex
Returns
bool:Trueif verticesuandvare connected,Falseotherwise
Complexities
depth
Returns the depth of a given node v.
Arguments
v: int: node
Returns
int: depth ofv(0-indexed)
Complexities
act
Applies a function to all vertices on the path between two given nodes u and v.
Arguments
u: int: first nodev: int: second nodef: U: endomorphism to apply to the path betweenuandv
Complexities
prod
Calculates the product of all vertices on the path between two given nodes u and v.
Arguments
u: int: first nodev: int: second node
Returns
T: product of all vertices on the path betweenuandv
