#K64542. Minimum Camera Coverage Problem
Minimum Camera Coverage Problem
Minimum Camera Coverage Problem
You are given a kingdom with n cities and m bidirectional roads connecting some pairs of these cities. In order to monitor all the roads, a camera must be installed in at least one city per connected component of the road network.
Your task is to determine the minimum number of cameras required, which is equal to the number of connected components in the graph.
Note: If there are no cities, no cameras are needed. Otherwise, if a city is isolated (not connected by any road), it counts as its own component.
One can formulate the problem mathematically as finding the number of connected components of an undirected graph \(G = (V, E)\), where \(V\) represents the cities and \(E\) represents the roads. The desired answer is:
\[ \text{Cameras} = \text{Number of Connected Components in } G \]inputFormat
The input is provided via standard input (stdin) with the following format:
- The first line contains two space-separated integers n and m, where n is the number of cities and m is the number of roads.
- Each of the next m lines contains two space-separated integers a and b, representing a bidirectional road between city a and city b.
outputFormat
Output a single integer to standard output (stdout) representing the minimum number of cameras required to monitor all the roads.
## sample5 5
1 2
2 3
3 4
4 5
5 1
1
</p>