61# ifndef TPL_LINK_CUT_TREE_WITH_EDGES_H
62# define TPL_LINK_CUT_TREE_WITH_EDGES_H
77 template <
typename VT = int,
81 requires LCTMonoid<EdgeMonoid, ET>
and LCTLazyTag<LazyTag, ET>
103 const size_t ha = std::hash<Node *>{}(key.a);
104 const size_t hb = std::hash<Node *>{}(key.b);
105 return ha ^ (hb + 0x9e3779b97f4a7c15ULL + (ha << 6) + (ha >> 2));
112 const EdgeKey & rhs)
const noexcept
133 return std::hash<Node *>{}(nd);
163 return it ==
nullptr ?
edge_recs_.size() : it->second;
185 return entry->second;
192 <<
"handle does not belong to this tree";
310 bool linked_u =
false;
311 bool linked_v =
false;
315 auto *entry =
edge_idx_.insert(key, idx);
420 return (internal_size + 1) / 2;
504 auto add_vert = [&](
Node * nd) {
505 if (node_to_idx.
search(nd) ==
nullptr)
512 for (
size_t i = 0; i <
edge_recs_.size(); ++i)
520 size_t nv = verts.
size();
522 for (
size_t i = 0; i < nv; ++i)
525 for (
size_t i = 0; i <
edge_recs_.size(); ++i)
528 auto * up = node_to_idx.
search(er.u);
529 auto * vp = node_to_idx.
search(er.v);
530 if (up !=
nullptr and vp !=
nullptr)
533 adj(up->second).
append({vp->second,
w});
534 adj(vp->second).
append({up->second,
w});
540 for (
size_t i = 0; i < nv; ++i)
543 size_t root_idx = node_to_idx.
search(
root)->second;
548 struct ScopedTreeOwner
552 TN * release()
noexcept { TN *
r =
root;
root =
nullptr;
return r; }
556 tn(root_idx) = guard.root;
563 while (qfront < queue.
size())
565 size_t ci = queue(qfront++);
566 for (
size_t j = 0; j < adj(ci).
size(); ++j)
567 if (
auto [ni,
w] = adj(ci)(j); tn(ni) ==
nullptr)
570 tn(ci)->insert_rightmost_child(tn(ni));
575 return guard.release();
580 template <
typename EdgeT =
int,
class Mono
id = DefaultMono
id<EdgeT>>
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
void destroy_tree(Tree &tree)
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
static Array create(const size_t n)
Create an array with n logical elements.
T & append(const T &data)
Append a copy of data
Pair * insert(const Key &key, const Data &data)
Inserts into the hash map the pair (key, record) indexed by key.
Pair * search(const Key &key) noexcept(nothrow_search< const Key & >)
Searches for the pair whose key is key.
Link-Cut Tree with edge weights and path aggregations.
const VT & lookup_vertex_val(Node *nd) const
const ET & get_edge_val(Node *u, Node *v)
Read the weight of an existing edge (u, v).
Tree_Node< ExportData > * export_to_tree_node(Node *root)
Export the represented tree rooted at root as a Tree_Node.
size_t num_edges() const noexcept
Number of edges in the forest.
size_t size() const noexcept
Total number of user vertices (excluding internal edge nodes).
void require_vertex(Node *x) const
Gen_Link_Cut_Tree_WithEdges()=default
Default constructor for an empty Link-Cut Tree with edges.
size_t num_components() const noexcept
Number of connected components.
DynMapHash< Node *, VT, NodePtrEq > vertex_vals_
void cut(Node *u, Node *v)
Remove the edge between u and v.
typename InternalLCT::Node Node
Opaque vertex handle (same type as internal LCT node).
void make_root(Node *x)
Make x the root of its represented tree.
void set_vertex_val(Node *x, const VT &val)
Replace the stored payload of a user vertex.
static size_t edge_key_hash(const EdgeKey &key) noexcept
size_t find_edge_idx(Node *u, Node *v) const noexcept
bool connected(Node *u, Node *v)
Test whether u and v are in the same tree.
Node * find_root(Node *x)
Find the root of the tree containing x.
static size_t node_ptr_hash(Node *const &nd) noexcept
ET path_query(Node *u, Node *v)
Aggregate edge values on the path from u to v.
Node * make_vertex(const VT &val=VT{})
Create an isolated vertex.
void remove_edge_at(size_t idx)
static EdgeKey make_edge_key(Node *u, Node *v) noexcept
Gen_Link_Cut_Tree_WithEdges(Gen_Link_Cut_Tree_WithEdges &&)=delete
Deleted move constructor.
Array< EdgeRec > edge_recs_
Gen_Link_Cut_Tree_WithEdges & operator=(Gen_Link_Cut_Tree_WithEdges &&)=delete
Deleted move assignment.
DynMapHash< EdgeKey, size_t, EdgeKeyEq > edge_idx_
void link(Node *u, Node *v, const ET &weight)
Add an edge (u, v) with weight weight.
Gen_Link_Cut_Tree_WithEdges(const Gen_Link_Cut_Tree_WithEdges &)=delete
Deleted copy constructor.
size_t path_size(Node *u, Node *v)
Number of user vertices on the path from u to v.
const VT & get_vertex_val(Node *x) const
Read the stored payload of a user vertex.
void set_edge_val(Node *u, Node *v, const ET &new_weight)
Update the weight of an existing edge (u, v).
Gen_Link_Cut_Tree_WithEdges & operator=(const Gen_Link_Cut_Tree_WithEdges &)=delete
Deleted copy assignment.
Dynamic forest with path queries via Link-Cut Trees.
void set_val(Node *x, const T &val)
Update the payload value of vertex x and recompute aggregates.
size_t path_size(Node *u, Node *v)
Number of vertices on the path from u to v.
void destroy_vertex(Node *x)
Destroy an isolated vertex.
const T & get_val(Node *x)
Read the payload value of vertex x.
void make_root(Node *x)
Make x the root of its represented tree.
T path_query(Node *u, Node *v)
Aggregate (under Monoid) over all vertex values on the path from u to v.
Node * find_root(Node *x)
Find the root of the represented tree containing x.
bool connected(Node *u, Node *v)
Test whether u and v are in the same represented tree.
Node * make_vertex(const T &val=T{})
Create an isolated vertex with payload val.
void link(Node *u, Node *v)
Add an edge between u and v, merging their trees.
void cut(Node *u, Node *v)
Remove the represented edge between u and v.
Forward declaration used by CRTP helpers before the full node definition.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
const unsigned long DefaultPrime
Default prime number used when no specific size is requested.
Default (no-op) monoid for connectivity-only usage.
Internal node of the Link-Cut Tree (opaque handle).
bool operator()(const EdgeKey &lhs, const EdgeKey &rhs) const noexcept
size_t operator()(const EdgeKey &key) const noexcept
bool operator==(const EdgeKey &) const noexcept=default
Data stored in each exported Tree_Node.
Node * lct_node
Back-pointer to the original LCT vertex handle.
ET parent_edge
Weight of the edge connecting this node to its parent.
VT vertex_value
Stored payload associated with the vertex.
bool operator()(Node *lhs, Node *rhs) const noexcept
No-op lazy tag (default — no deferred path updates).
Dynamic set implementations based on hash tables.
Link-Cut Tree: a dynamic forest with path queries.