#K79377. Finding Connected Components in a Forest

    ID: 35295 Type: Default 1000ms 256MiB

Finding Connected Components in a Forest

Finding Connected Components in a Forest

You are given a number of trees labeled from 1 to \(n\) and a list of connections, where each connection represents a bidirectional link between two trees. Your task is to determine the number of isolated groups (connected components) in the forest.

More formally, consider a graph \(G=(V,E)\) where \(V\) is the set of \(n\) trees and \(E\) is the set of edges connecting the trees. Two trees are in the same group if there exists a path connecting them. The answer is the number of connected components in \(G\).

Input/Output: Read the input from standard input (stdin) and write the output to standard output (stdout).

inputFormat

The first line contains two integers \(n\) and \(m\) where \(n\) is the number of trees and \(m\) is the number of connections.

The following \(m\) lines each contain two integers \(u\) and \(v\), indicating that there is an undirected connection between tree \(u\) and tree \(v\).

outputFormat

Output a single integer representing the number of connected components in the forest.

## sample
5 3
1 2
2 3
4 5
2