Link-cut Tree Algorithm

link / cut trees divide each tree in the represented forest into vertex-disjoint paths, where each path is represented by an auxiliary data structure (often splay trees, though the original paper predates splay trees and thus uses biased binary search trees).Given a node in one of the trees, unplug it (and its subtree) from the tree of which it is part. attach a node to another node as its child.

Link-cut Tree source code, pseudocode and analysis