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
- Check if longest connected component forms a palindrome in undirected graph
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements
- Ways to select one or more pairs from two different sets
- Number of connected components in a 2-D matrix of strings
- Minimize the number of weakly connected nodes
- Check if the length of all connected components is a Fibonacci number
- Number of connected components in a doubly linked list
- Convert undirected connected graph to strongly connected directed graph
- Maximum number of edges among all connected components of an undirected graph
- Program to count Number of connected components in an undirected graph
- Minimum number of Water to Land conversion to make two islands connected in a Grid
- Min number of moves to traverse entire Matrix through connected cells with equal values
- Minimum number of reversals to reach node 0 from every other node
- Find the number of ways to divide number into four parts such that a = c and b = d
- Ways to represent a number as a sum of 1's and 2's
- Number of ways to pair people
- Number of ways to get even sum by choosing three numbers from 1 to N
- Number of ways to go from one point to another in a grid
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.