119 const size_t n = cd.
size();
120 const size_t INF = std::numeric_limits<size_t>::max() / 4;
128 const std::array
nodes_arr = { n0, n1, n2, n3, n4, n5, n6,
n7,
n8,
n9,
n10 };
130 "Village-0",
"Village-1",
"Village-2",
"Village-3",
"Village-4",
131 "Village-5",
"Village-6",
"Village-7",
"Village-8",
"Village-9",
138 for (
size_t i = 0; i < n; ++i)
144 for (
size_t i = 0; i <
nodes_arr.size(); ++i)
155 for (
size_t i = 0; i < n; ++i)
168 [&](
const size_t c,
const size_t d,
const size_t)
176 auto query = [&](
const size_t u)
181 [&](
const size_t c,
const size_t d,
const size_t)
194 for (
size_t v = 0; v < n; ++v)
196 ans = std::min(
ans,
hld.distance_id(u, v));
202 std::cout << std::format(
"=== Emergency Response Network (Centroid Decomposition) ===\n\n");
203 std::cout << std::format(
"Centroid root: {} (id={})\n\n",
207 std::cout << std::format(
"Centroid chain for Village-10:\n");
210 [&](
const size_t c,
const size_t d,
const size_t k)
212 std::cout << std::format(
" k={} centroid={} dist={}\n", k, label_by_id(c), d);
215 std::cout << std::format(
"\nActivate team at Village-0 and Village-8\n");
221 const size_t u = cd.
id_of(node);
222 const size_t ans = query(u);
224 std::cout << std::format(
" nearest({:<10}) = {}\n",
label_by_id(u),
ans);
228 std::cout << std::format(
"\nActivate new team at Village-10\n");
233 const size_t u = cd.
id_of(node);
234 const size_t ans = query(u);
236 std::cout << std::format(
" nearest({:<10}) = {}\n",
label_by_id(u),
ans);
240 std::cout << std::format(
"\nAll assertions passed.\n");
Heavy-Light and Centroid decomposition on rooted trees represented as Aleph graphs.
WeightedDigraph::Node Node
int main()
Demonstrates centroid-decomposition-based dynamic nearest-center queries on a fixed tree.
static Array create(size_t n)
Create an array with n logical elements.
Centroid decomposition over a rooted tree represented as an Aleph graph.
size_t centroid_root_id() const noexcept
Root centroid ID of the centroid tree (or NONE if empty).
void for_each_centroid_ancestor_id(const size_t node, F &&f) const
Visit each centroid ancestor of node ID.
size_t id_of(const Node *node) const
size_t size() const noexcept
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)
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.
Arc of graph implemented with double-linked adjacency lists.
Generic graph and digraph implementations.