Union find and priority queue