ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::LinkCutTree< T, Op, Policy > Class Template Reference

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

Detailed description

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
class ADS::LinkCutTree< T, Op, Policy >

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

Member typedef documentation

◆ EdgeErased

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
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

◆ EdgeInserted

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
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

◆ modifications

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
using ADS::LinkCutTree< T, Op, Policy >::modifications
Initial value:
mod::Inserted< int, int, T > NodeInserted
a node added, its number as handle and key, with its value
Definition LinkCutTree.h:132
mod::Inserted< std::uint64_t, std::pair< int, int > > EdgeInserted
an edge added, by edgeOf() and the pair of its ends
Definition LinkCutTree.h:136
mod::Erased< std::uint64_t, std::pair< int, int > > EdgeErased
an edge removed, by edgeOf() and the pair of its ends
Definition LinkCutTree.h:138
mod::Assigned< int, T > ValueChanged
the value of a node changed
Definition LinkCutTree.h:134
anything may have changed: forget and recompute
Definition Modification.h:708
a list of types, the events of a structure [see Modification.h]
Definition Modification.h:482

the events: a node added, a value changed, an edge added or removed, anything changed at the end of a mute

◆ NodeInserted

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
using ADS::LinkCutTree< T, Op, Policy >::NodeInserted = mod::Inserted< int , int , T >

a node added, its number as handle and key, with its value

◆ Subject

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
using ADS::LinkCutTree< T, Op, Policy >::Subject = typename Policy::template bind< modifications , LinkCutTree >

the subject

◆ Value

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
using ADS::LinkCutTree< T, Op, Policy >::Value = T

the type of the values

◆ ValueChanged

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
using ADS::LinkCutTree< T, Op, Policy >::ValueChanged = mod::Assigned< int , T >

the value of a node changed

Constructor and destructor documentation

◆ LinkCutTree() [1/2]

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
ADS::LinkCutTree< T, Op, Policy >::LinkCutTree ( int n = 0,
const T & id = T(),
Op op = Op() )
explicit

the forest of \( n \) nodes with no edge, each of value \( \mathit{id} \), the identity of op

Precondition
\( n \ge 0 \)

◆ LinkCutTree() [2/2]

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
template<class... O>
requires ( sizeof...( O ) > 0 )
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...

Precondition
\( n \ge 0 \)

Member function documentation

◆ addNode()

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
int ADS::LinkCutTree< T, Op, Policy >::addNode ( const T & x)

a new node of value \( x \), alone, and its number

◆ aggregate()

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
T ADS::LinkCutTree< T, Op, Policy >::aggregate ( int u,
int v )
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}} \]

Precondition
\( u \) and \( v \) are nodes of the same tree

◆ attach()

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
template<class O>
requires ( Subject::dynamic )
int ADS::LinkCutTree< T, Op, Policy >::attach ( O & o)
inline

attaches the observer \( o \) and gives its number

Precondition
no batch is open

◆ connected()

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
bool ADS::LinkCutTree< T, Op, Policy >::connected ( int u,
int v )
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}} \]

Precondition
\( u \) and \( v \) are nodes

◆ cut()

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
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}} \]

Precondition
\( [ u , v ] \) is an edge of the forest

◆ cutParent()

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
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}} \]

Precondition
\( v \) is a node

◆ detach()

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
void ADS::LinkCutTree< T, Op, Policy >::detach ( int id)
inline

detaches the observer number \( \mathit{id} \)

Precondition
it is attached, no batch is open

◆ edgeOf()

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
constexpr std::uint64_t ADS::LinkCutTree< T, Op, Policy >::edgeOf ( int u,
int v )
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

◆ ends()

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
constexpr std::pair< int, int > ADS::LinkCutTree< T, Op, Policy >::ends ( int u,
int v )
inlinestaticnodiscardconstexpr

the ends of the edge \( [ u , v ] \), the least first

◆ evert()

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
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}} \]

Precondition
\( v \) is a node

◆ findRoot()

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
int ADS::LinkCutTree< T, Op, Policy >::findRoot ( int v)
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}} \]

Precondition
\( v \) is a node

◆ lca()

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
int ADS::LinkCutTree< T, Op, Policy >::lca ( int u,
int v )
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}} \]

Precondition
\( u \) and \( v \) are nodes of the same tree

◆ link()

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
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}} \]

Precondition
\( u \) and \( v \) are nodes of two distinct trees

◆ n()

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
int ADS::LinkCutTree< T, Op, Policy >::n ( void ) const
inlinenodiscard

the number of nodes

◆ setValue()

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
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

Precondition
\( v \) is a node

◆ splayed()

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
bool ADS::LinkCutTree< T, Op, Policy >::splayed ( void ) const
inlinenodiscard

whether the splay trees are in use

◆ subject() [1/2]

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
Subject & ADS::LinkCutTree< T, Op, Policy >::subject ( void )
inlinenodiscard

the subject, for ADS::batch() and ADS::quiet()

◆ subject() [2/2]

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
const Subject & ADS::LinkCutTree< T, Op, Policy >::subject ( void ) const
inlinenodiscard

the subject

◆ useSplay()

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
void ADS::LinkCutTree< T, Op, Policy >::useSplay ( void )

the splay trees from now on, whatever the lengths of the paths, in \( O( n ) \) time

◆ value()

template<class T = long long, class Op = std::plus< T >, class Policy = NoObservers>
const T & ADS::LinkCutTree< T, Op, Policy >::value ( int v) const
nodiscard

the value of \( v \)

Precondition
\( v \) is a node

The documentation for this class was generated from the following file: