Given a N-ary tree consisting of **N** nodes and a matrix **edges[][]** consisting of **N – 1** edges of the form **(X, Y)** denoting the edge between node **X** and node **Y** and an array **col[]** consisting of values:

**0:**Uncolored node.**1:**Node colored red.**2:**Node colored blue.

The task is to find the number of subtrees of the given tree which consists of only single-colored nodes.

**Examples:**

Input:

N = 5, col[] = {2, 0, 0, 1, 2},

edges[][] = {{1, 2}, {2, 3}, {2, 4}, {2, 5}}

Output:1

Explanation:

A subtree of node 4 which is {4} has no blue vertex and contains only one red vertex.

Input:

N = 5, col[] = {1, 0, 0, 0, 2},

edges[][] = {{1, 2}, {2, 3}, {3, 4}, {4, 5}}

Output:4

Explanation:

Below are the subtrees with the given property:

- Subtree with root node value 2 {2, 3, 4, 5}
- Subtree with root node value 3 {3, 4, 5}
- Subtree with root node value 4 {4, 5}
- Subtree with root node value 5 {5}

**Approach:** The given problem can be solved using Depth First Search Traversal. The idea is to calculate the number of red and blue nodes in each subtree using DFS for the given tree. Once calculated, count the number of subtrees containing only **blue** colored nodes and only **red** colored nodes.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to implement DFS traversal ` `void` `Solution_dfs(` `int` `v, ` `int` `color[], ` `int` `red, ` ` ` `int` `blue, ` `int` `* sub_red, ` ` ` `int` `* sub_blue, ` `int` `* vis, ` ` ` `map<` `int` `, vector<` `int` `> >& adj, ` ` ` `int` `* ans) ` `{ ` ` ` ` ` `// Mark node v as visited ` ` ` `vis[v] = 1; ` ` ` ` ` `// Traverse Adj_List of node v ` ` ` `for` `(` `int` `i = 0; i < adj[v].size(); ` ` ` `i++) { ` ` ` ` ` `// If current node is not visited ` ` ` `if` `(vis[adj[v][i]] == 0) { ` ` ` ` ` `// DFS call for current node ` ` ` `Solution_dfs(adj[v][i], color, ` ` ` `red, blue, ` ` ` `sub_red, sub_blue, ` ` ` `vis, adj, ans); ` ` ` ` ` `// Count the total red and blue ` ` ` `// nodes of children of its subtree ` ` ` `sub_red[v] += sub_red[adj[v][i]]; ` ` ` `sub_blue[v] += sub_blue[adj[v][i]]; ` ` ` `} ` ` ` `} ` ` ` ` ` `if` `(color[v] == 1) { ` ` ` `sub_red[v]++; ` ` ` `} ` ` ` ` ` `// Count the no. of red and blue ` ` ` `// nodes in the subtree ` ` ` `if` `(color[v] == 2) { ` ` ` `sub_blue[v]++; ` ` ` `} ` ` ` ` ` `// If subtree contains all ` ` ` `// red node & no blue node ` ` ` `if` `(sub_red[v] == red ` ` ` `&& sub_blue[v] == 0) { ` ` ` `(*ans)++; ` ` ` `} ` ` ` ` ` `// If subtree contains all ` ` ` `// blue node & no red node ` ` ` `if` `(sub_red[v] == 0 ` ` ` `&& sub_blue[v] == blue) { ` ` ` `(*ans)++; ` ` ` `} ` `} ` ` ` `// Function to count the number of ` `// nodes with red color ` `int` `countRed(` `int` `color[], ` `int` `n) ` `{ ` ` ` `int` `red = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(color[i] == 1) ` ` ` `red++; ` ` ` `} ` ` ` `return` `red; ` `} ` ` ` `// Function to count the number of ` `// nodes with blue color ` `int` `countBlue(` `int` `color[], ` `int` `n) ` `{ ` ` ` `int` `blue = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(color[i] == 2) ` ` ` `blue++; ` ` ` `} ` ` ` `return` `blue; ` `} ` ` ` `// Function to create a Tree with ` `// given vertices ` `void` `buildTree(` `int` `edge[][2], ` ` ` `map<` `int` `, vector<` `int` `> >& m, ` ` ` `int` `n) ` `{ ` ` ` `int` `u, v, i; ` ` ` ` ` `// Traverse the edge[] array ` ` ` `for` `(i = 0; i < n - 1; i++) { ` ` ` ` ` `u = edge[i][0] - 1; ` ` ` `v = edge[i][1] - 1; ` ` ` ` ` `// Create adjacency list ` ` ` `m[u].push_back(v); ` ` ` `m[v].push_back(u); ` ` ` `} ` `} ` ` ` `// Function to count the number of ` `// subtree with the given condition ` `void` `countSubtree(` `int` `color[], ` `int` `n, ` ` ` `int` `edge[][2]) ` `{ ` ` ` ` ` `// For creating adjacency list ` ` ` `map<` `int` `, vector<` `int` `> > adj; ` ` ` `int` `ans = 0; ` ` ` ` ` `// To store the count of subtree ` ` ` `// with only blue and red color ` ` ` `int` `sub_red[n + 3] = { 0 }; ` ` ` `int` `sub_blue[n + 3] = { 0 }; ` ` ` ` ` `// visted array for DFS Traversal ` ` ` `int` `vis[n + 3] = { 0 }; ` ` ` ` ` `// Count the number of red ` ` ` `// node in the tree ` ` ` `int` `red = countRed(color, n); ` ` ` ` ` `// Count the number of blue ` ` ` `// node in the tree ` ` ` `int` `blue = countBlue(color, n); ` ` ` ` ` `// Function Call to build tree ` ` ` `buildTree(edge, adj, n); ` ` ` ` ` `// DFS Traversal ` ` ` `Solution_dfs(0, color, red, blue, ` ` ` `sub_red, sub_blue, ` ` ` `vis, adj, &ans); ` ` ` ` ` `// Print the final count ` ` ` `cout << ans; ` `} ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 5; ` ` ` `int` `color[] = { 1, 0, 0, 0, 2 }; ` ` ` `int` `edge[][2] = { { 1, 2 }, ` ` ` `{ 2, 3 }, ` ` ` `{ 3, 4 }, ` ` ` `{ 4, 5 } }; ` ` ` ` ` `countSubtree(color, N, edge); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** O(N)

**Auxiliary Space:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find Count of Single Valued Subtrees
- Count subtrees that sum up to a given value x only using single recursive function
- Minimum nodes to be colored in a Graph such that every node has a colored neighbour
- Count of subtrees in a Binary Tree having XOR value K
- Count of subtrees in a Binary Tree having bitwise OR value K
- Count of distinct colors in a subtree of a Colored Tree with given min frequency for Q queries
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Count of all prime weight nodes between given nodes in the given Tree
- Calculate number of nodes in all subtrees | Using DFS
- Subtrees formed after bursting nodes
- Count the nodes in the given tree whose weight is even
- Count the nodes in the given tree whose weight is prime
- Count the nodes of the given tree whose weight has X as a factor
- Count the number of nodes at given level in a tree using BFS.
- Determine the count of Leaf nodes in an N-ary tree
- Count the nodes in the given tree whose weight is even parity
- Count the nodes in the given tree whose weight is a power of two
- Count the nodes in the given tree whose sum of digits of weight is odd
- Count Non-Leaf nodes in a Binary Tree
- Count the number of nodes at a given level in a tree using DFS

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.