#K50912. Counting Distinct Creature Groups

    ID: 28970 Type: Default 1000ms 256MiB

Counting Distinct Creature Groups

Counting Distinct Creature Groups

You are given n creatures and m connections between them. Each connection is represented by a pair of integers \(u\) and \(v\) indicating that creature \(u\) and creature \(v\) belong to the same group. Note that a connection can be a self-loop (\(u = v\)), which should be handled normally. Your task is to determine the total number of distinct groups formed.

In more formal terms, given an undirected graph with \(n\) nodes and \(m\) edges, compute the number of connected components in the graph.

Input Example: For instance, if \(n = 5\) and there are 3 connections: (1,2), (2,3), (4,5), then there are 2 groups: one containing creatures 1, 2, 3 and another containing creatures 4, 5.

inputFormat

The first line of input contains two integers (n) and (m), representing the number of creatures and the number of connections respectively. This is followed by (m) lines, each containing a pair of integers (u) and (v) which denote a connection between creature (u) and creature (v).

outputFormat

Output a single integer representing the number of distinct groups of creatures.## sample

5 3
1 2
2 3
4 5
2