Prim’s algorithm
Choose bridges that connect the islands for the lowest total cost. Start with one island and grow the connected group from there.
When a tree has no outgoing edge, start the next component. A disconnected map produces a forest.
Scanning for the next vertex takes O(V² + E) time overall. The answer lists bridge endpoint pairs.