#K35162. Minimum Monitor Placement
Minimum Monitor Placement
Minimum Monitor Placement
You are given an undirected network of n computers and m bidirectional connections between them. In order to monitor the activity on this network, you may place a monitor on any computer. Once placed on a computer, the monitor covers all computers in the same connected component (i.e. all computers that can reach each other via a series of connections). Your task is to determine the minimum number of monitors required to cover all computers in the network.
In other words, you need to find the number of connected components in the network. Note that a computer with no connections to any other computer is considered to be in its own connected component.
The problem can be formally expressed as follows: Given a graph with n vertices and m edges, find the number of connected components. This can be mathematically represented as:
$$ monitors = \text{number of connected components} $$
inputFormat
The input is given via standard input (stdin
) and has the following format:
n m u1 v1 u2 v2 ... um vm
Here, the first line contains two integers, n (the number of computers) and m (the number of connections). The next m lines each contain two integers u and v, indicating that there is a bidirectional connection between computer u and computer v.
outputFormat
Output a single integer to the standard output (stdout
): the minimum number of monitors required to cover all the computers in the network.
4 3
1 2
2 3
3 4
1