Number of Connected Components in an Undirected Graph

Medium Solved

Description

You are given an integer n representing the number of nodes, and an array edges where edges[i] = [a, b] indicates an undirected edge between nodes a and b.

Return the total number of connected components in the graph.

Input format:

  • Line 1: Integer n
  • Line 2: Edges array as JSON

Examples

Input:
5
[[0,1],[1,2],[3,4]]

Output:
2
Input:
5
[[0,1],[1,2],[2,3],[3,4]]

Output:
1

Note:

Print only the integer result.

No submissions yet.

Discuss DFS, BFS, Union-Find, and graph traversal techniques.

Test Cases