Hi, everyone!
Today I want to present my novel idea of finding $$$LCA$$$ of two nodes in at most $$$O(N + \sum_{j=1}^{Q} \log_2(\max(\text{depth}_{u_j}, \text{depth}_{v_j}) + 1))$$$ in offline. As we can see, algorithm depends heavily on the depth of queries, so in random trees it might work faster and memory is also pure $$$O(N + Q)$$$.
Prerequisites
- Euler tour technique
- Basic binary search
- Understanding tree
From the time complexity and prerequisites you might have guessed that we use ancestors of nodes, it is correct! Using properties of DFS we can keep ancestors sorted by depth in $$$O(N)$$$, example pseudocode:
dfs(v):
ancestors.add(v)
for u in a[v]: dfs(u)
ancestors.pop()
This is true because the moment we finished the subtree of $$$v$$$ node, we no longer can have it as a ancestor for other nodes. Since it is always sorted by depth, which is what we need for our binary search. Our task transforms into finding right-most node in our ancestors, which is ancestor of both $$$u$$$ and $$$v$$$. However we don't need out-order to check, because if current query's node partner was already visited in our DFS, then we can find LCA using only in-order.
Verified code on a problem: https://cses.fi/problemset/task/1688/
Thanks for reading, I hope you have a nice day.









I found about the thing called LPD(longest-path-decomposition), and I am wondering whether this with parallel binary search for each node's queries on the paths of LPD can optimize the time complexity(we are doing a lot of useless binary searches), essentially we only need stack of ancestors for the leaf in the LPD parts. I think it should become something like:
$$$O\left( N + \sum_{p \in \text{paths}} Q_p \cdot \log_2(|p|) \right)$$$
Where $$$Q_p$$$ is number of queries in that LPD path, of course everything here is theoritical, I wonder whether someone can calculate time complexity in a random tree, or whether it is correct.
The average distance between two random nodes in a random tree is asymptotically approximately $$$\sqrt{\frac{n\pi}{2}}-1$$$. Therefore, in random trees there is not much improvement, as $$$\log\left(\sqrt{\frac{n\pi}{2}}-1\right) \approx \frac{1}{2}\log(n)$$$.
For the idea in the blog, yes it is true. It worked 2x faster than normal binary jumping approach on https://cses.fi/problemset/task/1688/ but I am more interested in this optimization: https://mirror.codeforces.com/blog/entry/153399?#comment-1362593 .
I am not sure I have the right idea, but it seems like you are doing HLD?
yes
orz Nasa