From f6e375290e896b95d7c4bbee466a12efa19fc5bb Mon Sep 17 00:00:00 2001
From: Bruno Freitas Tissei <bft15@inf.ufpr.br>
Date: Mon, 26 Aug 2019 11:24:14 -0300
Subject: [PATCH] Small fixes and improvements

Signed-off-by: Bruno Freitas Tissei <bft15@inf.ufpr.br>
---
 algorithms/graph/hld.cpp |  4 ++--
 algorithms/graph/lca.cpp | 16 +++++++---------
 2 files changed, 9 insertions(+), 11 deletions(-)

diff --git a/algorithms/graph/hld.cpp b/algorithms/graph/hld.cpp
index b6b58e4..73908ce 100644
--- a/algorithms/graph/hld.cpp
+++ b/algorithms/graph/hld.cpp
@@ -88,14 +88,14 @@ struct HLD {
     for (; head[a] != head[b]; b = par[head[b]]) {
       if (dep[head[a]] > dep[head[b]])
         swap(a, b);
-      ans = seg.op(ans, seg.query(pos[head[b]], pos[b]));
+      ans = seg.func(ans, seg.query(pos[head[b]], pos[b]));
     }
 
     if (dep[a] > dep[b])
       swap(a, b);
 
     // Remove "+ 1" when values are associated with vertices
-    return seg.op(ans, seg.query(pos[a] + 1, pos[b]));
+    return seg.func(ans, seg.query(pos[a] + 1, pos[b]));
   }
  
   // Updates value of i-th edge (or vertice)
diff --git a/algorithms/graph/lca.cpp b/algorithms/graph/lca.cpp
index 51bd1cf..d16ddec 100644
--- a/algorithms/graph/lca.cpp
+++ b/algorithms/graph/lca.cpp
@@ -22,10 +22,12 @@
 vector<ii> graph[MAX];
 int par[MAX][LOG], cost[MAX][LOG];
 
+template <class F = function<T(const T&, const &T)>>
 struct LCA {
+  F func;
   vector<int> h;
 
-  LCA(int N) : h(N)
+  LCA(int N, F &func) : h(N), func(func)
   { init(); }
 
   void init() {
@@ -34,10 +36,6 @@ struct LCA {
     dfs(0); // root is 0
   }
 
-  int op(int a, int b) {
-    return a + b; // or max(a, b)
-  }
-
   void dfs(int v, int p = -1, int c = 0) {
     par[v][0] = p;
     cost[v][0] = c;
@@ -48,7 +46,7 @@ struct LCA {
     for (int i = 1; i < LOG; ++i)
       if (par[v][i - 1] != -1) {
         par[v][i] = par[par[v][i - 1]][i - 1];
-        cost[v][i] = op(cost[v][i], op(cost[v][i-1],
+        cost[v][i] = func(cost[v][i], func(cost[v][i-1],
               cost[par[v][i-1]][i-1]));
       }
 
@@ -65,7 +63,7 @@ struct LCA {
 
     for (int i = LOG - 1; i >= 0; --i)
       if (par[a][i] != -1 && h[par[a][i]] >= h[b]) {
-        ans = op(ans, cost[a][i]);
+        ans = func(ans, cost[a][i]);
         a = par[a][i];
       }
 
@@ -79,7 +77,7 @@ struct LCA {
 
     for (int i = LOG - 1; i >= 0; --i)
       if (par[a][i] != -1 && par[a][i] != par[b][i]) {
-        ans = op(ans, op(cost[a][i], cost[b][i]));
+        ans = func(ans, func(cost[a][i], cost[b][i]));
         a = par[a][i];
         b = par[b][i];
       }
@@ -88,7 +86,7 @@ struct LCA {
       if (a == b) 
         return ans;
       else 
-        return op(ans, op(cost[a][0], cost[b][0]));
+        return func(ans, func(cost[a][0], cost[b][0]));
     #else
       return par[a][0];
     #endif
-- 
GitLab