Given a tree with N vertices numbered 1 through N with vertex 1 as root vertex and N – 1 edges. We have to color exactly k number of vertices and count the number of uncolored vertices between root vertex and every colored vertex. We have to include the root vertex in the count if it is not colored. The task to maximize the number of uncolored vertices occurring between the path from root vertex and the colored vertices.
Input : 1 / | \ / | \ 2 3 4 / \ \ / \ \ 5 6 7 k = 4 Output : 7 Explanation: If we color vertex 2, 5, 6 and 7, the number of uncolored vertices between the path from root to colored vertices is maximum which is 7. Input : 1 / \ / \ 2 3 / / 4 k = 1 Output : 2
To solve the above-mentioned problem we observe that if a vertex is chosen to be uncolored then its parent must be chosen to be uncolored. Then we can calculate how many uncolored vertices we will get if we choose a certain path to the colored vertex. Simply calculate the difference between the number of vertices between root to each vertex and the number of vertices that occur below the current vertex. Take the largest k of all the difference and calculate the sum. Use nth_element stl to get an O(n) solution.
Below is the implementation of the above approach:
Time Complexity: O(N)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find if there is a path between two vertices in a directed graph
- Find the number of islands | Set 1 (Using DFS)
- Union-Find Algorithm | Set 2 (Union By Rank and Path Compression)
- Dijkstra's shortest path algorithm | Greedy Algo-7
- Shortest Path in Directed Acyclic Graph
- Articulation Points (or Cut Vertices) in a Graph
- Eulerian path and circuit for undirected graph
- Fleury's Algorithm for printing Eulerian Path or Circuit
- Longest Path in a Directed Acyclic Graph
- Find maximum number of edge disjoint paths between two vertices
- Some interesting shortest path questions | Set 1
- Shortest path with exactly k edges in a directed and weighted graph
- Vertex Cover Problem | Set 1 (Introduction and Approximate Algorithm)
- Optimal read list for given number of days
- Number of Triangles in an Undirected Graph
- Number of Triangles in Directed and Undirected Graphs
- Check whether given degrees of vertices represent a Graph or Tree
- Shortest Path in a weighted Graph where weight of an edge is 1 or 2
- Printing Paths in Dijkstra's Shortest Path Algorithm
- Dijkstra’s shortest path algorithm using set in STL
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.