// use union find // u : root // G : graph (children lists) // 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[], VI q[], int f[], int a[], bool c[]) { makeSet(f,u); a[u]=u; for(int v : G[u]) { // for each children of us Tarjan(v, G, q, f, a, c); unite(f,u,v); a[find(f,u)] = u; } c[u] = true; for(int v : q[u]) if(c[v]) cout<<"LCA("<