Short answer: Tarjan’s offline LCA answers a batch of lowest-common-ancestor queries during one depth-first search, using union-find. When the search finishes a node, the node is united with its parent and the parent’s set is labeled with the parent. A query between the finished node and a node already visited is answered by the label of that visited node’s set. The time is O(n + q · α(n)) for q queries. The queries must be known before the search starts.
What the problem actually asks
Binary lifting spends O(n log n) preprocessing even if only a few queries arrive later, and it answers them online. A batch that is known in advance can be answered in essentially linear time. The depth-first search visits each edge a constant number of times. The missing ingredient is a way to name, for a node already finished, the highest ancestor that has not finished. Union-find with an ancestor label stores exactly that.
The algorithm is offline. A query that arrives after the search has finished is not answered by another lookup in the same structure. The batch is the input.
What the algorithm guarantees
Every query pair is reported once, when the later of the two nodes finishes, and the reported ancestor is the LCA. Time is the depth-first search plus a union and a find per tree edge and per query, which is O(n + q α(n)) with path compression and union by rank. Extra memory is the union-find arrays and the list of queries incident to each node.
The union happens after the node’s children are processed, and the set’s ancestor pointer is then set to the node’s parent. Uniting before the subtree finishes, or labeling the set with the node instead of the parent at the wrong moment, answers with a descendant. The usual presentation unites a finished node to its parent and sets find-root’s ancestor to the parent.
The working steps
- Attach each query to both of its endpoints, so either endpoint can see the pair.
- Run a depth-first search. On entering a node, mark it seen and create a singleton set whose ancestor label is itself.
- Recurse into each child. When a child returns, union the child with the current node and set the ancestor label of the merged set to the current node.
- After the children, scan the queries stored at this node. If the other endpoint is already seen, the answer is the ancestor label of that endpoint’s set.
- The other endpoint will not answer the same query again if each query is emitted only when the second endpoint is processed. Tie the emission to a check that the other side is already seen.
The nearly linear bound is for a static tree and a fixed batch. Online queries belong to binary lifting or to an Euler-tour sparse table. The union-find here does not maintain a spanning forest under insertions. Calling this procedure Tarjan’s strongly connected components is a different algorithm that shares an author and a stack-free confusion. This one is the LCA batch.
Approaches that look simpler and fail
| Approach | Why it fails when a query is answered before both nodes are seen |
|---|---|
| Answer a query on entering the first endpoint | The other endpoint may be unvisited, and the union-find has not built the ancestor chain between them. The answer is emitted when the second endpoint is seen. |
| Forget to relabel the set with the current node after uniting a child | The find root would still name the child. The LCA would come back as a descendant of the true answer. |
| Accept a new query after the search and call find | The ancestor labels were correct only at the moment the second endpoint finished. The structure is not an online oracle. |
| Use this timing for strongly connected components | Strong components use a low-link stack on a directed graph. The union-find in this algorithm tracks unfinished ancestors in a tree. |
Online LCA belongs to binary lifting or an Euler tour with a range-minimum query. Strongly connected components belong to Tarjan’s other one-pass algorithm or to Kosaraju. This procedure is the offline batch.
A working example
This Python fragment unions each finished child into the current node and labels the set with that node. A query is answered when its other endpoint is already seen. On the tree with root 0 and queries (3, 4), (3, 2), (4, 1) the answers are 1 0 1.
children = [[1, 2], [3, 4], [], [], []]
queries = [(3, 4), (3, 2), (4, 1)]
attached = [[] for _ in range(5)]
for index, (a, b) in enumerate(queries):
attached[a].append((b, index))
attached[b].append((a, index))
parent = list(range(5))
ancestor = list(range(5))
seen = [False] * 5
answer = [None] * len(queries)
def find(x):
if parent[x] != x:
parent[x] = find(parent[x])
return parent[x]
def dfs(u):
seen[u] = True
for v in children[u]:
dfs(v)
parent[find(v)] = u
ancestor[u] = u
for other, index in attached[u]:
if seen[other] and answer[index] is None:
answer[index] = ancestor[find(other)]
dfs(0)
assert answer == [1, 0, 1]
print(*answer)
Where it is used
Tarjan’s offline LCA answers a batch of deepest-common-ancestor questions once the tree and every query are known. A compiler or a directory tool that can wait for the whole batch can use it. Binary lifting answers the same question online.
When you can drop this algorithm
Drop the offline pass when queries arrive interleaved with the answers, or when the tree is not static. Use binary lifting. Keep Tarjan’s offline LCA when the whole batch is known and the nearly linear bound matters.
Why this stays published
We publish this so a search for Tarjan’s offline LCA returns the union-find labeling during depth-first search and the requirement that the batch is known first. Tarjan’s offline algorithm answers a batch of LCA queries in almost linear time by labeling union-find sets with ancestors during one depth-first search.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
