Improved the UF by using weighted quick union.