#K85282. Minimum Lights to Decorate

    ID: 36607 Type: Default 1000ms 256MiB

Minimum Lights to Decorate

Minimum Lights to Decorate

In this problem, you are given a description of a village with houses and roads connecting them. Each house is represented as a node and each road as an undirected edge in a graph. The goal is to determine the minimum number of lights required to illuminate all the houses. A single light can cover all houses within a connected component of the graph.

More formally, given an undirected graph with \(n\) vertices and \(m\) edges, you need to compute the number of connected components in the graph. Each connected component requires one light.

For example, if the village has 5 houses connected by 4 roads forming a single connected component, then only 1 light is needed, whereas if the houses are isolated or partially connected, the number of lights required will equal the number of connected components.

inputFormat

The input is read from stdin and has the following format:

T
n m
u1 v1
u2 v2
... (m lines)
... (repeat for T test cases)

Here, \(T\) is the number of test cases. For each test case, \(n\) denotes the number of houses and \(m\) denotes the number of roads. Each of the next \(m\) lines contains two integers \(u\) and \(v\) indicating there is a road between house \(u\) and house \(v\).

outputFormat

For each test case, output a single integer on a new line representing the minimum number of lights required (i.e., the number of connected components in the graph). The output is written to stdout.

## sample
2
5 4
1 2
1 3
3 4
4 5
3 2
1 2
2 3
1

1

</p>