#C7650. Counting Connected Components

    ID: 51545 Type: Default 1000ms 256MiB

Counting Connected Components

Counting Connected Components

You are given an undirected graph with \(n\) nodes and \(m\) edges. The graph is described by \(n\) and \(m\) on the first line, followed by \(m\) lines where each line contains two integers \(u\) and \(v\) representing an undirected edge between nodes \(u\) and \(v\). Your task is to count the number of connected components in the graph.

A connected component is defined as a set of nodes where each node is reachable from every other node in the same set. Formally, if there exists a path between any two nodes in the component, then they belong to the same connected component.

Input Format: The graph is given in the following form:

  • The first line contains two integers \(n\) and \(m\) where \(n\) (\(1 \leq n \leq 10^5\)) is the number of nodes and \(m\) (\(0 \leq m \leq 10^5\)) is the number of edges.
  • The next \(m\) lines each contain two integers \(u\) and \(v\) (\(1 \leq u,v \leq n\)) representing an edge between node \(u\) and node \(v\).

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

inputFormat

The input is read from standard input (stdin). The first line contains two integers \(n\) and \(m\), representing the number of nodes and edges respectively. The following \(m\) lines each contain two space-separated integers \(u\) and \(v\) representing an undirected edge between nodes \(u\) and \(v\).

outputFormat

Output a single integer to standard output (stdout) which is the number of connected components in the graph.

## sample
1 0
1