Given a tree, where each vertex V has a value A[V] stored in it. The task is to find the minimum number of operations required to make the values stored in all vertices of the tree equal to zero.
Each Operation consists of following 2 steps:
- Select a Subtree such that the subtree include vertex 1.
- Increase/Decrease the value of all vertices of the subtree by 1.
Consider the following tree:
Note: The number in the vertex denotes the vertex number and A[V] denotes the value of the vertex as explained above.
For the following Tree we perform the following 3 operations to make the values all the vertices
equal to zero:
Note: The vertices in black represent the subtree selected.
We can solve this problem using Dynamic Programming.
Let dp[i] denote the number of operations where any subtree rooted at i is selected and the value of all the vertices is increased by 1.
Similarly, dp[i] denotes the number of operations where any subtree rooted at i is selected and the value of all the vertices is decreased by 1.
For all the leaves we can easily compute dp[i] and dp[i] if say a leaf node V is such that A[V] = 0 for some leaf node U, i.e dp[i] = A[V] and dp[i] = 0
Now if we are in some non leaf node say v, we look at all of its children, if say increase operation is applied Xi times for a child i of V then we need to apply max(Xi for all children i of node v), increase operations for any subtree rooted at v. Similarly we do the same for Decrease operations for the node V.
The answer is the sum of the increase and decrease operations for node 1 since the operations are applied only on subtrees having node 1.
Below is the implementation of the above approach:
Time Complexity : O(V), where V is the number of nodes in the tree.
Auxiliary Space : O(V), where V is the number of nodes in the tree.
- Find K vertices in the graph which are connected to at least one of remaining vertices
- Maximize the number of uncolored vertices appearing along the path from root vertex and the colored vertices
- Make a tree with n vertices , d diameter and at most vertex degree k
- Minimum cost to colour a Tree with no 3 adjacent vertices having same colour
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Print all the Paths of a Binary Tree whose XOR is non-zero
- Minimize operations required to make each element of Array equal to it's index value
- Minimum increment or decrement operations required to make the array sorted
- Minimum pair merge operations required to make Array non-increasing
- Check whether given degrees of vertices represent a Graph or Tree
- Check if a path exists in a tree with K vertices present or are at most at a distance D
- Possible edges of a tree for given diameter, height and vertices
- Maximize the sum of products of the degrees between any two vertices of the tree
- Find the number of distinct pairs of vertices which have a distance of exactly k in a tree
- Minimum number of edges between two vertices of a Graph
- Minimum initial vertices to traverse whole matrix with given conditions
- Maximum and minimum isolated vertices in a graph
- Minimum number of edges between two vertices of a graph using DFS
- Count all possible paths between two vertices
- Construct a graph from given degrees of all 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 firstname.lastname@example.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.
Improved By : rituraj_jain