← 返回 tesla 的题目列表Count Connected Components in Undirected Graph
类型:online_judge
tesla
Given an undirected graph with nodes and edges. Nodes are denoted by integers, and edges are pairs of nodes. You need to calculate the number of connected components in the graph, which are subgraphs consisting of nodes that are reachable from each other but not from other nodes. For instance, given the edges [(0,1), (0,2), (3,4)], there are 2 connected components. Write a function to solve this problem. Input: two integers n and m, representing the number of nodes and edges, respectively, and a list edges that denotes m edges. Output: an integer denoting the number of connected components in the graph.
Input format:
The first line contains two integers n and m, representing the number of nodes and edges.
The next m lines each contain two integers ui and vi, representing an undirected edge (ui, vi).
Output format:
An integer representing the number of connected components in the graph.
Example
Input
5 3
0 1
0 2
3 4