Optimal algorithms for ?nding nearest common ancestors in dynamic trees

We consider the problem of nding the nearest common ancestor of two given nodes x and y (denoted by nca(x; y)) in a collection of dynamic rooted trees. Interspersed with nca-queries are on-line commands link(x; y) where x but not necessarily y is a tree root. The eeect of a command link(x; y) is to combine the trees containing x and y by making y the parent of x. We present a pointer machine algorithm which performs n link and m nca in time O(n + m log log n), matching a lower-bound by Harel and Tarjan (SIAM J.