#C8627. Minimum New Acquaintances
Minimum New Acquaintances
Minimum New Acquaintances
You are given n wizards and m pairs of initial acquaintances. Each pair (u, v) means that wizard u is initially acquainted with wizard v. In order to ensure that any wizard can communicate (i.e. exchange spells) with any other wizard, you may need to introduce new acquaintances between wizards in different groups.
Your task is to determine the minimum number of new acquaintances needed such that the network of wizards becomes fully connected. In other words, if there are k disconnected groups (connected components) in the network, you need to add at least \(k-1\) new acquaintance(s) to connect all components.
Input and Output Formats: The input will be read from stdin and the output should be printed to stdout.
inputFormat
The first line contains two integers n and m separated by a space, where n is the number of wizards and m is the number of initial acquaintance pairs.
Each of the following m lines contains two integers u and v (1-indexed) indicating that wizard u is acquainted with wizard v.
outputFormat
Output a single integer representing the minimum number of new acquaintances needed to ensure that all wizards are connected.
Recall that if there are \(k\) connected components, you need to add \(k - 1\) acquaintances.
## sample5 3
1 2
2 3
4 5
1
</p>