Browse Source

simplify LCA

Olivier Marty 8 years ago
parent
commit
1d114e2efd
1 changed files with 8 additions and 9 deletions
  1. 8 9
      code/LCA.cpp

+ 8 - 9
code/LCA.cpp

@@ -1,10 +1,10 @@
 // use union find
 // u : root
 // G : graph (children lists)
-// q : queries q.first[u] : set of v for asking (u, v), q.second miroir
+// q : queries q[u] : set of v for asking (u, v) (must be symmetric)
 // f : union-find array, a : ancestor, c : colour (init false)
 void makeSet(int *C, int i) { C[i] = i; }
-void Tarjan(int u, VI G[], pair<VI*, VI*> q, int f[], int a[], bool c[]) {
+void Tarjan(int u, VI G[], VI q[], int f[], int a[], bool c[]) {
   makeSet(f,u);
   a[u]=u;
   for(int v : G[u]) { // for each children of us
@@ -13,16 +13,15 @@ void Tarjan(int u, VI G[], pair<VI*, VI*> q, int f[], int a[], bool c[]) {
     a[find(f,u)] = u;
   }
   c[u] = true;
-  for(VI* qu : {q.first, q.second})
-    for(int v : qu[u])
-      if(c[v])
-        cout<<"LCA("<<u<<','<<v<<")="<<a[find(f,v)]<<endl;
+  for(int v : q[u])
+    if(c[v])
+      cout<<"LCA("<<u<<','<<v<<")="<<a[find(f,v)]<<endl;
 }
 void test() {
   VI G[6];
   G[0].push_back(1); G[0].push_back(2); G[2].push_back(3); G[2].push_back(4); G[3].push_back(5);
-  VI q1[6], q2[6];
-  q1[3].push_back(2); q2[2].push_back(3);
+  VI q[6];
+  q[3].push_back(2); q[2].push_back(3);
   int f[6]; int a[6]; bool c[6] = {0, 0, 0, 0, 0, 0};
-  Tarjan(0, G, make_pair(q1, q2), f, a, c);
+  Tarjan(0, G, q, f, a, c);
 }