#K2536. Minimum Additional Roads for Complete Reachability
Minimum Additional Roads for Complete Reachability
Minimum Additional Roads for Complete Reachability
You are tasked with designing a futuristic transport system for a city. The city has N places connected by M bidirectional roads. In addition, some roads may intersect; however, the provided intersection information does not directly impact connectivity between the places.
Your mission is to ensure that every place is reachable from every other place by possibly adding the minimum number of additional roads. In other words, if the current network of roads results in several disconnected groups (components), you need to add enough roads to connect these groups together. Mathematically, if the city forms \( k \) disconnected components, you must add at least \( k-1 \) additional roads.
Input and Output Specification: You are required to read from standard input (stdin) and output your answer to standard output (stdout).
Note: Even though intersection data is provided, it is not utilized in determining connectivity between places.
inputFormat
The first line contains two integers N and M, where N is the number of places and M is the number of roads.
The next M lines each contain two integers u and v indicating that there is a direct road between places u and v.
The following line contains an integer I, representing the number of intersections.
The next I lines each contain two integers x and y indicating that the x-th and y-th roads intersect. This data is provided for completeness but does not affect the connectivity calculations.
outputFormat
Output a single integer representing the minimum number of additional roads required so that every place is reachable from every other place.
## sample3 3
1 2
2 3
3 1
2
1 2
2 3
0