Kruskal MST algorithm.
Use DisjointSet data structure.