Given a tree consisting of N nodes numbered from [1, N] and is colored either black(denoted by 1) or green(denoted by 0), the task is to count the number of sequences of length K [a1, a2, ..aK] such that the path taken between consecutive nodes is the shortest one and the edges covered consist of at least one black edge. Since the answer can be large, print it to modulo of 109+7.
Input: N = 4, K = 4
Since there is no black edge in the tree. There are no such sequences.
Input: N = 3, K = 3
All the 33 sequences except for (1, 1, 1), (2, 2, 2) and (3, 3, 3) are included in the answer.
The idea is to count the number of sequences of length K such that no black edge is covered. Let the count be temp. Then (NK) – temp is the required answer. temp can be easily calculated by removing the black edges and then calculating the size of different components of the resultant graph.
Follow the steps below:
- Initialize the value of ans as NK.
- Construct a graph G by adding only green edges.
- Perform a DFS traversal of the graph and keep subtracting (sizeK) from the ans where size is the number of nodes in different components of the graph G.
Below is the implementation of the above approach:
Time Complexity: O(N), Since DFS traversal requires O(Vertices + Edges) == O(N + (N-1)) == O(N) ) complexity.
- Sub-strings that start and end with one character and have at least one other
- Count sequences of length K having each term divisible by its preceding term
- Find two equal subsequences of maximum length with at least one different index
- Convert a number of length N such that it contains any one digit at least 'K' times
- Maximum difference of count of black and white vertices in a path containing vertex V
- Count subarrays consisting of only 0's and only 1's in a binary array
- Count of subtrees from an N-ary tree consisting of single colored nodes
- Count of Root to Leaf Paths consisting of at most M consecutive Nodes having value K
- Count number of times each Edge appears in all possible paths of a given Tree
- Queries to find the count of shortest paths in a Tree that contains a given edge
- Count pairs of parentheses sequences such that parentheses are balanced
- Longest Subsequence with at least one common digit in every element
- Numbers that are bitwise AND of at least one non-empty sub-array
- Find K vertices in the graph which are connected to at least one of remaining vertices
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Count of strings that become equal to one of the two strings after one removal
- Find the k-th string in lexicographical order consisting of n-2 X's and 2 Y's
- Longest subsequence consisting of alternate vowels and consonants
- Check if an array has some palindromic subsequence of length at least 3
- Find maximum number of edge disjoint paths between two vertices
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.