#C11738. Count Connected Components in an Undirected Graph

    ID: 41087 Type: Default 1000ms 256MiB

Count Connected Components in an Undirected Graph

Count Connected Components in an Undirected Graph

You are given an undirected graph represented by n nodes and m edges. The graph is specified using an edge list where each edge connects two nodes. Your task is to compute the number of connected components in the graph.

A connected component is defined as a maximal set of nodes such that there exists a path between any two nodes in the set. In mathematical terms, for a graph \(G=(V,E)\), you need to find the number of disjoint subsets \(V_1, V_2, \dots, V_k\) of vertices such that for every \(u, v \in V_i\), there is a path connecting \(u\) and \(v\), and for any \(u \in V_i, v \in V_j\) with \(i \neq j\), no such path exists.

The input is read from stdin and the output is printed to stdout.

inputFormat

The first line contains two integers n and m, where n is the number of nodes (numbered from 0 to n-1) and m is the number of edges.

The following m lines each contain two space-separated integers u and v, representing an undirected edge connecting node u with node v.

outputFormat

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

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