Wires to Add
What is the minimum number of new links needed so that everyone ends up in a single circle?
Input: the first line has N and M; each of the next M lines has an edge u v (1-based).
Output: components − 1.
Constraints:
- 1 ≤ N ≤ 104
- 0 ≤ M ≤ 2×104
- multiple edges and self-loops may appear
Sample Tests
Input 1
5 2 1 2 3 4
Output 1
2
Input 2
1 0
Output 2
0
Discussion (opens after you solve — no spoilers before)
No comments yet.