55 cout <<
"=== Example 1: Dynamic connectivity ===" <<
endl;
79 cout <<
" linking node 2 to node 3..." <<
endl;
84 cout <<
" cutting edge (2, 3)..." <<
endl;
96 cout <<
"=== Example 2: Rerooting ===" <<
endl;
111 cout <<
" Before reroot: find_root(E) = node "
115 cout <<
" After make_root(C): find_root(A) = node "
117 cout <<
" After make_root(C): find_root(E) = node "
120 cout <<
" Path size from A to E = " << lct.
path_size(a, e) <<
endl;
130 cout <<
"=== Example 3: LCA ===" <<
endl;
168 cout <<
"=== Example 4: Path aggregates ===" <<
endl;
193 cout <<
" after set_val(2, 100): path_sum(0, 4) = "
241 cout <<
"=== Example 5: Lazy path updates ===" <<
endl;
247 auto * n0 = lct.make_vertex(0
LL);
248 auto * n1 = lct.make_vertex(0
LL);
249 auto * n2 = lct.make_vertex(0
LL);
250 auto * n3 = lct.make_vertex(0
LL);
251 auto * n4 = lct.make_vertex(0
LL);
258 cout <<
" Initial path_sum(0, 4) = " << lct.path_query(n0, n4) <<
endl;
261 lct.path_apply(n0, n4, 10LL);
262 cout <<
" After +10 on path(0,4): sum = " << lct.path_query(n0, n4) <<
endl;
265 lct.path_apply(n1, n3, 5LL);
266 cout <<
" After +5 on path(1,3): sum(0,4) = "
267 << lct.path_query(n0, n4) <<
endl;
268 cout <<
" Sub-path sum(1,3) = " << lct.path_query(n1, n3) <<
endl;
270 cout <<
" Individual values: ";
271 for (
auto *
nd : {n0, n1, n2, n3, n4})
274 cout << lct.get_val(
nd) <<
" ";
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.
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.
Node * lca(Node *u, Node *v)
Lowest common ancestor of u and v.
void cut(Node *u, Node *v)
Remove the represented edge between u and v.
static void example_connectivity()
static void example_path_aggregates()
static void example_lazy_updates()
static void example_lca()
static void example_rerooting()
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.
Additive lazy tag: adds a delta to every node on a path.
Link-Cut Tree: a dynamic forest with path queries.