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. **Examples:**

Input :1 / | \ / | \ 2 3 4 / \ \ / \ \ 5 6 7 k = 4Output :7Explanation: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 = 1Output :2

**Approach:**

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:

## CPP

`// C++ program to Maximize the number` `// of uncolored vertices occurring between` `// the path from root vertex and the colored vertices` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Comparator function` `bool` `cmp(` `int` `a, ` `int` `b)` `{` ` ` `return` `a > b;` `}` `class` `graph ` `{` ` ` `vector<vector<` `int` `> > g;` ` ` `vector<` `int` `> depth;` ` ` `vector<` `int` `> subtree;` ` ` `int` `* diff;` `public` `:` ` ` `// Constructor` ` ` `graph(` `int` `n)` ` ` `{` ` ` `g = vector<vector<` `int` `> >(n + 1);` ` ` `depth = vector<` `int` `>(n + 1);` ` ` `subtree = vector<` `int` `>(n + 1);` ` ` `diff = ` `new` `int` `[n + 1];` ` ` `}` ` ` `// Function to push edges` ` ` `void` `push(` `int` `a, ` `int` `b)` ` ` `{` ` ` `g[a].push_back(b);` ` ` `g[b].push_back(a);` ` ` `}` ` ` `// function for dfs traversal` ` ` `int` `dfs(` `int` `v, ` `int` `p)` ` ` `{` ` ` `// Store depth of vertices` ` ` `depth[v] = depth[p] + 1;` ` ` `subtree[v] = 1;` ` ` `for` `(` `auto` `i : g[v]) {` ` ` `if` `(i == p)` ` ` `continue` `;` ` ` `// Calculate number of vertices` ` ` `// in subtree of all vertices` ` ` `subtree[v] += dfs(i, v);` ` ` `}` ` ` `// Computing the difference` ` ` `diff[v] = depth[v] - subtree[v];` ` ` `return` `subtree[v];` ` ` `}` ` ` `// Function that print maximum number of` ` ` `// uncolored vertices occur between root vertex` ` ` `// and all colored vertices` ` ` `void` `solution(` `int` `n, ` `int` `k)` ` ` `{` ` ` `// Computing first k largest difference` ` ` `nth_element(diff + 1, diff + k, diff + n + 1, cmp);` ` ` `int` `sum = 0;` ` ` `for` `(` `int` `i = 1; i <= k; i++) {` ` ` `sum += diff[i];` ` ` `}` ` ` `// Print the result` ` ` `cout << sum << ` `"\n"` `;` ` ` `}` `};` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 7;` ` ` `int` `k = 4;` ` ` `// initialise graph` ` ` `graph g(N);` ` ` `g.push(1, 2);` ` ` `g.push(1, 3);` ` ` `g.push(1, 4);` ` ` `g.push(3, 5);` ` ` `g.push(3, 6);` ` ` `g.push(4, 7);` ` ` `g.dfs(1, 0);` ` ` `g.solution(N, k);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to Maximize the number` `# of uncolored vertices occurring between` `# the path from root vertex and the colored vertices` `g ` `=` `[]` `depth ` `=` `[]` `subtree ` `=` `[]` `diff ` `=` `[]` `# Constructor` `def` `graph(n):` ` ` `global` `g, depth, subtree, diff` ` ` `g ` `=` `[[] ` `for` `i ` `in` `range` `(n ` `+` `1` `)]` ` ` `depth ` `=` `[` `0` `]` `*` `(n ` `+` `1` `)` ` ` `subtree ` `=` `[` `0` `]` `*` `(n ` `+` `1` `)` ` ` `diff ` `=` `[` `0` `]` `*` `(n ` `+` `1` `)` `# Function to push edges` `def` `push(a, b):` ` ` `global` `g` ` ` `g[a].append(b)` ` ` `g[b].append(a)` `# function for dfs traversal` `def` `dfs(v, p):` ` ` `global` `depth, subtree, g, diff` ` ` `# Store depth of vertices` ` ` `depth[v] ` `=` `depth[p] ` `+` `1` ` ` `subtree[v] ` `=` `1` ` ` `for` `i ` `in` `g[v]:` ` ` `if` `(i ` `=` `=` `p):` ` ` `continue` ` ` `# Calculate number of vertices` ` ` `# in subtree of all vertices` ` ` `subtree[v] ` `+` `=` `dfs(i, v)` ` ` `# Computing the difference` ` ` `diff[v] ` `=` `depth[v] ` `-` `subtree[v]` ` ` `return` `subtree[v]` `# Function that prmaximum number of` `# uncolored vertices occur between root vertex` `# and all colored vertices` `def` `solution(n, k):` ` ` `global` `diff` ` ` `# Computing first k largest difference` ` ` `diff ` `=` `sorted` `(diff)[::` `-` `1` `]` ` ` ` ` `# nth_element(diff + 1, diff + k, diff + n + 1, cmp)` ` ` `sum` `=` `2` ` ` `for` `i ` `in` `range` `(` `1` `, k ` `+` `1` `):` ` ` `sum` `+` `=` `diff[i]` ` ` `# Prthe result` ` ` `print` `(` `sum` `)` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `N ` `=` `7` ` ` `k ` `=` `4` ` ` `# initialise graph` ` ` `graph(N)` ` ` `push(` `1` `, ` `2` `)` ` ` `push(` `1` `, ` `3` `)` ` ` `push(` `1` `, ` `4` `)` ` ` `push(` `3` `, ` `5` `)` ` ` `push(` `3` `, ` `6` `)` ` ` `push(` `4` `, ` `7` `)` ` ` `dfs(` `1` `, ` `0` `)` ` ` `solution(N, k)` `# This code is contributed by mohit kumar 29.` |

*chevron_right*

*filter_none*

**Output:**

7

**Time Complexity:** 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.