diff --git a/algorithms/graph/hld.cpp b/algorithms/graph/hld.cpp
index b6b58e473b96e5e70dedfc082a95c5ad43bad4dc..73908ce0699743c5f380c875b979d8c8d4258e14 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 51bd1cf3940cd655f36684e2568803882c973cb4..d16ddec7782c7f1e1300287459abbb501ad0fba2 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