#K45402. Fast Track Connections for Districts
Fast Track Connections for Districts
Fast Track Connections for Districts
You are given a nation with n districts and m roads connecting some pairs of these districts. The districts and roads form an undirected graph. Some districts may be disconnected from others.
Your task is to determine the minimum number of additional fast-track connections required so that every district becomes reachable from any other district. In other words, you need to connect all the separate connected components in the graph with the least number of additional roads.
Hint: The answer equals the number of connected components minus one. Use a graph traversal algorithm like BFS or DFS to count the connected components.
inputFormat
The first line contains two integers n and m — the number of districts and the number of roads, respectively. Each of the following m lines contains two space-separated integers representing a road connecting two districts.
outputFormat
Output a single integer representing the minimum number of fast-track connections required to connect all districts.## sample
1 0
0