|
ADS
Algorithms and Data Structures
|
a forest of dynamic trees whose nodes have values of the type T, aggregated on the paths by the commutative monoid Op, its changes told through Policy More...
#include <ADS/tree/LinkCutTree.h>
Public types | |
| using | Value = T |
| the type of the values | |
| using | NodeInserted = mod::Inserted< int , int , T > |
| a node added, its number as handle and key, with its value | |
| using | ValueChanged = mod::Assigned< int , T > |
| the value of a node changed | |
| using | EdgeInserted = mod::Inserted< std::uint64_t , std::pair< int , int > > |
| an edge added, by edgeOf() and the pair of its ends | |
| using | EdgeErased = mod::Erased< std::uint64_t , std::pair< int , int > > |
| an edge removed, by edgeOf() and the pair of its ends | |
| using | modifications |
| the events: a node added, a value changed, an edge added or removed, anything changed at the end of a mute | |
| using | Subject = typename Policy::template bind< modifications , LinkCutTree > |
| the subject | |
Public member functions | |
| LinkCutTree (int n=0, const T &id=T(), Op op=Op()) | |
| the forest of \( n \) nodes with no edge, each of value \( \mathit{id} \), the identity of op | |
| template<class... O> requires ( sizeof...( O ) > 0 ) | |
| LinkCutTree (int n, const T &id, Op op, O &... o) | |
| the forest of \( n \) nodes with no edge, each of value \( \mathit{id} \), the identity of op, the static observers o... | |
| int | n (void) const |
| the number of nodes | |
| int | addNode (const T &x) |
| a new node of value \( x \), alone, and its number | |
| const T & | value (int v) const |
| the value of \( v \) | |
| void | setValue (int v, const T &x) |
| the value of \( v \) set to \( x \), in \( O( \log n ) \) amortized time | |
| void | evert (int v) |
| \( v \) made the root of its tree, in \( O( \log n ) \) amortized time | |
| int | findRoot (int v) |
| the root of the tree of \( v \), in \( O( \log n ) \) amortized time | |
| void | useSplay (void) |
| the splay trees from now on, whatever the lengths of the paths, in \( O( n ) \) time | |
| bool | splayed (void) const |
| whether the splay trees are in use | |
| bool | connected (int u, int v) |
| whether \( u \) and \( v \) are in the same tree, in \( O( \log n ) \) amortized time | |
| void | link (int u, int v) |
| the edge \( [ u , v ] \), which joins two trees, \( u \) made a child of \( v \), in \( O( \log n ) \) amortized time | |
| void | cut (int u, int v) |
| the edge \( [ u , v ] \) removed, in \( O( \log n ) \) amortized time | |
| bool | cutParent (int v) |
| the edge from \( v \) to its parent removed, in the trees as rooted now, \( v \) the root of its new tree; false if \( v \) is a root | |
| int | lca (int u, int v) |
| the lowest common ancestor of \( u \) and \( v \) in the trees as rooted now, in \( O( \log n ) \) amortized time | |
| T | aggregate (int u, int v) |
| the aggregate by Op of the values of the nodes on the path between \( u \) and \( v \), ends included, in \( O( \log n ) \) amortized time; the root of the tree of \( u \) becomes \( u \) | |
| Subject & | subject (void) |
| the subject, for ADS::batch() and ADS::quiet() | |
| const Subject & | subject (void) const |
| the subject | |
| template<class O> requires ( Subject::dynamic ) | |
| int | attach (O &o) |
| attaches the observer \( o \) and gives its number | |
| void | detach (int id) |
| detaches the observer number \( \mathit{id} \) | |
Static public member functions | |
| static constexpr std::uint64_t | edgeOf (int u, int v) |
| the handle of the edge \( [ u , v ] \), the same for \( [ v , u ] \): the least end in the high 32 bits, the other in the low ones | |
| static constexpr std::pair< int, int > | ends (int u, int v) |
| the ends of the edge \( [ u , v ] \), the least first | |
a forest of dynamic trees whose nodes have values of the type T, aggregated on the paths by the commutative monoid Op, its changes told through Policy
| using ADS::LinkCutTree< T, Op, Policy >::EdgeErased = mod::Erased< std::uint64_t , std::pair< int , int > > |
an edge removed, by edgeOf() and the pair of its ends
| using ADS::LinkCutTree< T, Op, Policy >::EdgeInserted = mod::Inserted< std::uint64_t , std::pair< int , int > > |
an edge added, by edgeOf() and the pair of its ends
| using ADS::LinkCutTree< T, Op, Policy >::modifications |
the events: a node added, a value changed, an edge added or removed, anything changed at the end of a mute
| using ADS::LinkCutTree< T, Op, Policy >::NodeInserted = mod::Inserted< int , int , T > |
a node added, its number as handle and key, with its value
| using ADS::LinkCutTree< T, Op, Policy >::Subject = typename Policy::template bind< modifications , LinkCutTree > |
the subject
| using ADS::LinkCutTree< T, Op, Policy >::Value = T |
the type of the values
| using ADS::LinkCutTree< T, Op, Policy >::ValueChanged = mod::Assigned< int , T > |
the value of a node changed
|
explicit |
the forest of \( n \) nodes with no edge, each of value \( \mathit{id} \), the identity of op
| ADS::LinkCutTree< T, Op, Policy >::LinkCutTree | ( | int | n, |
| const T & | id, | ||
| Op | op, | ||
| O &... | o ) |
the forest of \( n \) nodes with no edge, each of value \( \mathit{id} \), the identity of op, the static observers o...
| int ADS::LinkCutTree< T, Op, Policy >::addNode | ( | const T & | x | ) |
a new node of value \( x \), alone, and its number
|
nodiscard |
the aggregate by Op of the values of the nodes on the path between \( u \) and \( v \), ends included, in \( O( \log n ) \) amortized time; the root of the tree of \( u \) becomes \( u \)
In pseudocode (Sleator and Tarjan, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{aggregate}( u , v ) \cr 2 & \quad \mathrm{evert}( u );\ \mathrm{access}( v );\ \textbf{return} \ \mathit{agg}[ v ] \quad \triangleright\ \text{the splay tree of} \text{ the path} \end{array}} \]
|
inline |
attaches the observer \( o \) and gives its number
|
nodiscard |
whether \( u \) and \( v \) are in the same tree, in \( O( \log n ) \) amortized time
In pseudocode (beyond the book): after \( \mathrm{access}( u ) \) the node \( u \) roots the splay tree of the path from the root of its tree, with no parent, and \( \mathrm{access}( v ) \) gives it one if and only if it climbs into that path:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{connected}( u , v ) \cr 2 & \quad \textbf{if}\ u = v\ \textbf{then return true} \cr 3 & \quad \mathrm{access}( u );\ \mathrm{access}( v ); \ \textbf{return}\ \mathit{par}[ u ] \ne \textbf{nil} \end{array}} \]
| void ADS::LinkCutTree< T, Op, Policy >::cut | ( | int | u, |
| int | v ) |
the edge \( [ u , v ] \) removed, in \( O( \log n ) \) amortized time
In pseudocode (Sleator and Tarjan, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{cut}( u , v ) \cr 2 & \quad \mathrm{evert}( u );\ \mathrm{access}( v ) \quad \triangleright\ \text{the path is } u , v \text{ alone} \cr 3 & \quad \mathit{left}[ v ] \gets \textbf{nil};\ \mathit{par}[ u ] \gets \textbf{nil} \end{array}} \]
| bool ADS::LinkCutTree< T, Op, Policy >::cutParent | ( | int | v | ) |
the edge from \( v \) to its parent removed, in the trees as rooted now, \( v \) the root of its new tree; false if \( v \) is a root
In pseudocode (Sleator and Tarjan, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{cutParent}( v ) \cr 2 & \quad \mathrm{access}( v ) \cr 3 & \quad \textbf{if}\ \mathit{left}[ v ] = \textbf{nil}\ \textbf{then} \ \textbf{return}\ \textbf{false} \cr 4 & \quad \mathit{par}[ \mathit{left}[ v ] ] \gets \textbf{nil}; \ \mathit{left}[ v ] \gets \textbf{nil};\ \textbf{return} \ \textbf{true} \end{array}} \]
|
inline |
detaches the observer number \( \mathit{id} \)
|
inlinestaticnodiscardconstexpr |
the handle of the edge \( [ u , v ] \), the same for \( [ v , u ] \): the least end in the high 32 bits, the other in the low ones
|
inlinestaticnodiscardconstexpr |
the ends of the edge \( [ u , v ] \), the least first
| void ADS::LinkCutTree< T, Op, Policy >::evert | ( | int | v | ) |
\( v \) made the root of its tree, in \( O( \log n ) \) amortized time
In pseudocode (Sleator and Tarjan, beyond the book), on access() below:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{evert}( v ) \cr 2 & \quad \mathrm{access}( v );\ \mathit{rev}[ v ] \gets \textbf{not} \ \mathit{rev}[ v ] \quad \triangleright\ \text{the path from the} \text{ root to } v \text{ reversed} \end{array}} \]
|
nodiscard |
the root of the tree of \( v \), in \( O( \log n ) \) amortized time
In pseudocode (Sleator and Tarjan, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{findRoot}( v ) \cr 2 & \quad \mathrm{access}( v );\ r \gets v \cr 3 & \quad \textbf{while}\ \mathit{left}[ r ] \ne \textbf{nil} \ \textbf{do}\ r \gets \mathit{left}[ r ] \quad \triangleright \ \text{the shallowest node of the path} \cr 4 & \quad \mathrm{splay}( r );\ \textbf{return}\ r \end{array}} \]
|
nodiscard |
the lowest common ancestor of \( u \) and \( v \) in the trees as rooted now, in \( O( \log n ) \) amortized time
In pseudocode (Sleator and Tarjan, beyond the book): the last node at which access( v ) enters the preferred path that access( u ) left:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lca}( u , v ) \cr 2 & \quad \mathrm{access}( u );\ \textbf{return}\ \mathrm{access}( v ) \end{array}} \]
| void ADS::LinkCutTree< T, Op, Policy >::link | ( | int | u, |
| int | v ) |
the edge \( [ u , v ] \), which joins two trees, \( u \) made a child of \( v \), in \( O( \log n ) \) amortized time
In pseudocode (Sleator and Tarjan, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{link}( u , v ) \cr 2 & \quad \mathrm{evert}( u );\ \mathit{par}[ u ] \gets v \quad \triangleright\ \text{a path-parent pointer} \end{array}} \]
|
inlinenodiscard |
the number of nodes
| void ADS::LinkCutTree< T, Op, Policy >::setValue | ( | int | v, |
| const T & | x ) |
the value of \( v \) set to \( x \), in \( O( \log n ) \) amortized time
|
inlinenodiscard |
whether the splay trees are in use
|
inlinenodiscard |
the subject, for ADS::batch() and ADS::quiet()
|
inlinenodiscard |
the subject
| void ADS::LinkCutTree< T, Op, Policy >::useSplay | ( | void | ) |
the splay trees from now on, whatever the lengths of the paths, in \( O( n ) \) time
|
nodiscard |
the value of \( v \)