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
- Queries to check if vertices X and Y are in the same Connected Component of an Undirected Graph
- Check if longest connected component forms a palindrome in undirected graph
- Convert undirected connected graph to strongly connected directed graph
- Find the maximum component size after addition of each edge to the 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
- Number of ways to select exactly K even numbers from given Array
- Ways to select one or more pairs from two different sets
- Largest component size in a graph formed by connecting non-co-prime nodes
- Number of connected components in a doubly linked list
- Minimize the number of weakly connected nodes
- Minimum number of Water to Land conversion to make two islands connected in a Grid
- Maximum number of edges among all connected components of an undirected graph
- Number of connected components in a 2-D matrix of strings
- Min number of moves to traverse entire Matrix through connected cells with equal values
- Maximum number of edges to be removed to contain exactly K connected components in the Graph
- Check if the length of all connected components is a Fibonacci number
- Program to count Number of connected components in an undirected graph
- Level of Each node in a Tree from source node (using BFS)
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.