Given a Tree with **N** vertices and **N – 1** edges where the vertices are numbered from **0 to N – 1**, and a vertex **V** present in the tree. It is given that each vertex in the tree has a colour assigned to it which is **either white or black** and the respective colours of the vertices is represented by an array **arr[]**. The task is to find the maximum difference between the number of white coloured vertices and the number of black coloured vertices from any possible subtree from the given tree that contains the given vertex **V**.

**Examples:**

Input:V = 0, arr[] = {'b', 'w', 'w', 'w', 'b', 'b', 'b', 'b', 'w'} Tree: 0 b / \ / \ 1 w 2 w / / \ / / \ 5 b w 3 4 b | | | | | | 7 b b 6 8 wOutput:2Explanation:We can take the subtree containing the vertex 0 which contains vertices 0, 1, 2, 3 such that the difference between the number of white and the number of black vertices is maximum which is equal to 2.Input:V = 2, arr[] = {'b', 'b', 'w', 'b'} Tree: 0 b / | \ / | \ 1 2 3 b w bOutput:1

**Approach:** The idea is to use the concept of dynamic programming to solve this problem.

- Firstly, make a vector for colour array and for white colour, push 1 and for black colour, push -1.
- Make an array dp[] to calculate the maximum possible difference between the number of white and black vertices in some subtree containing the vertex V.
- Now, traverse through the tree using depth first search traversal and update the values in dp[] array.
- Finally, the minimum value present in the dp[] array is the required answer.

Below is the implementation of the above approach:

`// C++ program to find maximum ` `// difference between count of ` `// black and white vertices in ` `// a path containing vertex V ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Defining the tree class ` `class` `tree { ` ` ` `vector<` `int` `> dp; ` ` ` `vector<vector<` `int` `> > g; ` ` ` `vector<` `int` `> c; ` ` ` `public` `: ` ` ` `// Constructor ` ` ` `tree(` `int` `n) ` ` ` `{ ` ` ` `dp = vector<` `int` `>(n); ` ` ` `g = vector<vector<` `int` `> >(n); ` ` ` `c = vector<` `int` `>(n); ` ` ` `} ` ` ` ` ` `// Function for adding edges ` ` ` `void` `addEdge(` `int` `u, ` `int` `v) ` ` ` `{ ` ` ` `g[u].push_back(v); ` ` ` `g[v].push_back(u); ` ` ` `} ` ` ` ` ` `// Function to perform DFS ` ` ` `// on the given tree ` ` ` `void` `dfs(` `int` `v, ` `int` `p = -1) ` ` ` `{ ` ` ` `dp[v] = c[v]; ` ` ` ` ` `for` `(` `auto` `i : g[v]) { ` ` ` `if` `(i == p) ` ` ` `continue` `; ` ` ` ` ` `dfs(i, v); ` ` ` ` ` `// Returning calculated maximum ` ` ` `// difference between white ` ` ` `// and black for current vertex ` ` ` `dp[v] += max(0, dp[i]); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Function that prints the ` ` ` `// maximum difference between ` ` ` `// white and black vertices ` ` ` `void` `maximumDifference(` `int` `v, ` ` ` `char` `color[], ` ` ` `int` `n) ` ` ` `{ ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// Condition for white vertex ` ` ` `if` `(color[i] == ` `'w'` `) ` ` ` `c[i] = 1; ` ` ` ` ` `// Condition for black vertex ` ` ` `else` ` ` `c[i] = -1; ` ` ` `} ` ` ` ` ` `// Calling dfs function for vertex v ` ` ` `dfs(v); ` ` ` ` ` `// Printing maximum difference between ` ` ` `// white and black vertices ` ` ` `cout << dp[v] << ` `"\n"` `; ` ` ` `} ` `}; ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `tree t(9); ` ` ` ` ` `t.addEdge(0, 1); ` ` ` `t.addEdge(0, 2); ` ` ` `t.addEdge(2, 3); ` ` ` `t.addEdge(2, 4); ` ` ` `t.addEdge(1, 5); ` ` ` `t.addEdge(3, 6); ` ` ` `t.addEdge(5, 7); ` ` ` `t.addEdge(4, 8); ` ` ` ` ` `int` `V = 0; ` ` ` ` ` `char` `color[] = { ` `'b'` `, ` `'w'` `, ` `'w'` `, ` ` ` `'w'` `, ` `'b'` `, ` `'b'` `, ` ` ` `'b'` `, ` `'b'` `, ` `'w'` `}; ` ` ` ` ` `t.maximumDifference(V, color, 9); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:** *O(N)*, where N is the number of vertices in the tree.

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:

- Maximize the number of uncolored vertices appearing along the path from root vertex and the colored vertices
- Check if every vertex triplet in graph contains two vertices connected to third vertex
- Check if incoming edges in a vertex of directed graph is equal to vertex itself or not
- Check if vertex X lies in subgraph of vertex Y for the given Graph
- Make a tree with n vertices , d diameter and at most vertex degree k
- Find K vertices in the graph which are connected to at least one of remaining vertices
- Sum of Nodes and respective Neighbors on the path from root to a vertex V
- Finding the path from one vertex to rest using BFS
- Number of pairs such that path between pairs has the two vertices A and B
- Find if there is a path between two vertices in a directed graph
- Longest path between any pair of vertices
- Check if a path exists in a tree with K vertices present or are at most at a distance D
- Find if there is a path between two vertices in a directed graph | Set 2
- Maximize shortest path between given vertices by adding a single edge
- Find if there is a path between two vertices in an undirected graph
- Count of node sequences of length K consisting of at least one black edge
- Find node U containing all nodes from a set V at atmost distance 1 from the path from root to U
- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
- Program to find the number of persons wearing white hat
- Queries to count connected components after removal of a vertex from a Tree

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.