#C9772. Minimum Communication Towers
Minimum Communication Towers
Minimum Communication Towers
You are given a set of cities and bidirectional roads connecting some pairs of cities. Each road has a weight, but the weight is not used in determining connectivity. Two cities are directly connected if there is a road between them, and indirectly connected if there is a path of roads linking them. In order to provide communication coverage to all cities, a communication tower is needed in each connected component. Your task is to determine the minimum number of communication towers required, which is simply the number of connected components in the graph.
Mathematically, if the cities form a graph \(G=(V,E)\), then the answer is the number of connected components \(C\), i.e., \[ \text{Towers} = C \]
The graph can be disconnected, and some cities might be isolated. For an isolated city, it forms a connected component by itself.
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains a single integer \(T\), the number of test cases.
- For each test case, 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.
- This is followed by \(M\) lines, each containing three space-separated integers \(u\), \(v\), and \(w\) representing a road between cities \(u\) and \(v\) with length \(w\). (Note that \(w\) is not used in connectivity calculations.)
outputFormat
For each test case, print a single integer on a new line — the minimal number of communication towers required (i.e., the number of connected components).
## sample1
3 2
1 2 4
2 3 5
1
</p>