#K94997. Island Groups
Island Groups
Island Groups
You are given a set of islands and some bridges connecting them. Two islands are considered to be in the same group if they are connected directly or indirectly by bridges. Your task is to determine the number of distinct island groups.
Formally, let (N) be the number of islands and (M) be the number of bridges. Each bridge connects two islands, (u) and (v). If there is no bridge connecting an island to any other, then it forms a group by itself. Use either graph traversal or union-find algorithms to count the total number of connected components (groups).
Input is provided via standard input (stdin) and the output should be printed to standard output (stdout).
inputFormat
The first line contains two integers (N) and (M) separated by a space, where (N) is the number of islands and (M) is the number of bridges. The following (M) lines each contain two integers (u) and (v) ((1 \le u, v \le N)) indicating that there is a bridge connecting island (u) and island (v).
outputFormat
Output a single integer representing the number of island groups.## sample
4 2
1 2
3 4
2