sample code using Union-Find