A persistent union-find data structure
Introduction to Algorithms