#K78277. Count Isolated Groups
Count Isolated Groups
Count Isolated Groups
You are given a company represented as a graph with N individuals (numbered from 1 to N) and M bidirectional friendship pairs. Two individuals are directly connected if there is a friendship between them. An isolated group is defined as a connected component in this graph, meaning a set of individuals where every individual is reachable from every other individual within the same group.
Your task is to compute the number of isolated groups in the company.
The relation can be expressed by the graph formula: Given a graph \(G=(V, E)\) where \(V\) is the set of nodes (individuals) and \(E\) is the set of undirected edges (friendships), you need to count the number of connected components in \(G\).
inputFormat
The input is given via standard input (stdin) in the following format:
- The first line contains two space-separated integers \(N\) and \(M\), where \(N\) is the number of individuals and \(M\) is the number of friendship pairs.
- The next \(M\) lines each contain two space-separated integers \(U\) and \(V\), representing a friendship between individual \(U\) and individual \(V\).
outputFormat
Output a single integer on standard output (stdout) representing the number of isolated groups (connected components) in the company.
## sample5 3
1 2
2 3
4 5
2