Given a graph with N nodes and M edges. The task is to find the number of ways to select a node from each connected component of the given graph.
(1, 4), (2, 4), (3, 4) are possible ways.
(1, 4, 5), (2, 4, 5), (3, 4, 5), (1, 4, 6), (2, 4, 6), (3, 4, 6) are possible ways.
Approach: A product of the number of nodes in each connected component is the required answer. Run a simple dfs to find the number of nodes in each connected component.
Below is the implementation of the above approach:
- Largest connected component on a grid
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Ways to select one or more pairs from two different sets
- Minimize the number of weakly connected nodes
- Number of connected components in a 2-D matrix of strings
- Number of connected components in a doubly linked list
- Maximum number of edges among all connected components of an undirected graph
- Program to count Number of connected components in an undirected graph
- Find the number of ways to divide number into four parts such that a = c and b = d
- Minimum cost path from source node to destination node via an intermediate node
- Ways to represent a number as a sum of 1's and 2's
- Number of ways to swap two bit of s1 so that bitwise OR of s1 and s2 changes
- Number of ways to get even sum by choosing three numbers from 1 to N
- Number of ways to reach the end of matrix with non-zero AND value
- Number of ways to pair people
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : rituraj_jain