Graph Theory37 sections · 1633 units
Open in Course

Cities and Roads

Codeforces 330B

I will give you the Cities and Roads task. You get NN cities and some roads, and you must add the fewest roads to make the graph connected. If the graph has KK connected components, you need K1K-1 roads to join them into one piece.

You can solve it by running a traversal from each unvisited node, counting components, and outputting the count minus one. You will use the same traversal idea in the next sections, so this problem sets your target.