From 8d63451a51329cfa8f32f7b652d1d14d12171b7b Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Tue, 17 Dec 2024 14:36:01 -0500 Subject: [PATCH] HLD golf (#282) --- content/graph/HLD.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/content/graph/HLD.h b/content/graph/HLD.h index 62eb33c5b..e198f45f2 100644 --- a/content/graph/HLD.h +++ b/content/graph/HLD.h @@ -26,8 +26,8 @@ template struct HLD { : N(sz(adj_)), adj(adj_), par(N, -1), siz(N, 1), rt(N),pos(N),tree(new Node(0, N)){ dfsSz(0); dfsHld(0); } void dfsSz(int v) { - if (par[v] != -1) adj[v].erase(find(all(adj[v]), par[v])); for (int& u : adj[v]) { + adj[u].erase(find(all(adj[u]), v)); par[u] = v; dfsSz(u); siz[v] += siz[u]; @@ -42,11 +42,11 @@ template struct HLD { } } template void process(int u, int v, B op) { - for (; rt[u] != rt[v]; v = par[rt[v]]) { - if (pos[rt[u]] > pos[rt[v]]) swap(u, v); + for (;; v = par[rt[v]]) { + if (pos[u] > pos[v]) swap(u, v); + if (rt[u] == rt[v]) break; op(pos[rt[v]], pos[v] + 1); } - if (pos[u] > pos[v]) swap(u, v); op(pos[u] + VALS_EDGES, pos[v] + 1); } void modifyPath(int u, int v, int val) {