#C8500. Minimum Lights to Illuminate the Park
Minimum Lights to Illuminate the Park
Minimum Lights to Illuminate the Park
You are given an undirected graph that represents a park. The park consists of n intersections (nodes) and m two-way streets (edges). In order to illuminate the park, you need to place lights at some intersections. A single light placed at any intersection will illuminate the entire connected component in which that intersection resides. Hence, the minimum number of lights required is equal to the number of connected components in the graph.
Formally, if the park is represented as an undirected graph \(G=(V,E)\) with \(|V|=n\) and \(|E|=m\), then the answer is the number of connected components in \(G\). Note that if \(n=0\), the answer is 0.
The input is given via standard input and the output should be written to standard output.
inputFormat
The first line contains an integer T
representing the number of test cases. Each test case is defined as follows:
- The first line of each test case contains two space-separated integers
n
andm
, representing the number of intersections and the number of streets, respectively. - Each of the next
m
lines contains two space-separated integersu
andv
indicating that there is an undirected edge (street) connecting intersectionsu
andv
.
If there are no edges (i.e. m=0
), then no additional lines follow for that test case.
outputFormat
For each test case, output a single integer on a new line — the minimum number of lights required to illuminate the park (i.e., the number of connected components in the graph).
## sample5
5 4
1 2
2 3
3 4
4 5
4 2
1 2
3 4
3 0
4 3
1 2
2 3
3 4
0 0
1
2
3
1
0
</p>