78 while (
hld.depth_of_id(u) >
hld.depth_of_id(v))
84 while (
hld.depth_of_id(v) >
hld.depth_of_id(u))
98 return sum + values(u);
114 for (
size_t v = 0; v <
hld.size(); ++v)
115 if (
hld.is_ancestor_id(u, v))
164 const std::array<const char *, 9> labels = {
165 "Central",
"North",
"West",
"South",
"N-A",
166 "N-B",
"S-A",
"S-A1",
"S-A2"};
169 const auto &
hld =
grid.decomposition();
172 for (
size_t i = 0; i <
hld.size(); ++i)
173 values(i) =
hld.node_of(i)->get_info();
175 std::cout <<
"=== Aurora Power Grid (HLD path queries) ===\n\n";
176 std::cout <<
"Node mapping (id -> label, base position):\n";
177 for (
size_t i = 0; i <
hld.size(); ++i)
178 std::cout << std::format(
" id={} {:<8} pos={}\n",
179 i, labels[i],
hld.position_of_id(i));
181 std::cout <<
"\nPath maintenance cost queries:\n";
190 const std::array<Query, 4>
queries = {{
191 {
na,
nb,
"N-A -> N-B"},
192 {
na,
sa2,
"N-A -> S-A2"},
199 const size_t u =
hld.id_of(q.u);
200 const size_t v =
hld.id_of(q.v);
202 const int ans =
grid.query_path_id(u, v);
205 std::cout << std::format(
" {:<18} HLD={:>3} brute={:>3}\n",
210 std::cout <<
"\nSubtree (service area) totals:\n";
213 const size_t id =
hld.id_of(x);
214 const int ans =
grid.query_subtree_id(
id);
216 std::cout << std::format(
" {:<8} subtree total = {:>3}\n",
221 std::cout <<
"\nBudget changes (point updates):\n";
222 std::cout <<
" +5 to S-A2, set West to 26\n";
235 std::cout << std::format(
" North -> S-A2 after update: HLD={} brute={}\n",
239 std::cout <<
"\nHow HLD splits path N-A -> S-A2 into base-array segments:\n";
245 std::cout << std::format(
" segment [{}, {}] ({})\n",
246 l,
r, (reversed ?
"reversed" :
"forward"));
249 std::cout <<
"\nAll assertions passed.\n";
Heavy-Light and Centroid decomposition on rooted trees represented as Aleph graphs.
WeightedDigraph::Node Node
Simple dynamic array with automatic resizing and functional operations.
static Array create(size_t n)
Create an array with n logical elements.
Segment-tree-powered path/subtree queries over an HLD layout.
Heavy-Light Decomposition over a rooted tree represented as an Aleph graph.
Graph implemented with double-linked adjacency lists.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
_Graph_Node Node
The graph type.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
int main()
Executable example demonstrating heavy-light decomposition on a small tree.
Main namespace for Aleph-w library functions.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Arc of graph implemented with double-linked adjacency lists.
Generic graph and digraph implementations.