给定一棵树,其中N个顶点编号为 1 到 N,顶点1 作为根顶点和N – 1 条边。我们必须为k个顶点着色,并计算根顶点和每个着色顶点之间未着色顶点的数量。如果根顶点没有着色,我们必须将其包含在计数中。最大化在从根顶点到有色顶点的路径之间出现的无色顶点数的任务。
例子:
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
方法:
为了解决上述问题,我们观察到如果一个顶点被选择为未着色,那么它的父节点必须被选择为未着色。然后我们可以计算如果我们选择一条通往有色顶点的特定路径,我们将得到多少个无色顶点。只需计算根到每个顶点之间的顶点数与出现在当前顶点下方的顶点数之间的差值。取所有差值中最大的 k 并计算总和。使用 nth_element stl 获得 O(n) 解决方案。
下面是上述方法的实现:
C++
// C++ program to Maximize the number
// of uncolored vertices occurring between
// the path from root vertex and the colored vertices
#include
using namespace std;
// Comparator function
bool cmp(int a, int b)
{
return a > b;
}
class graph
{
vector > g;
vector depth;
vector subtree;
int* diff;
public:
// Constructor
graph(int n)
{
g = vector >(n + 1);
depth = vector(n + 1);
subtree = vector(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;
}
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]
# Print the 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.
输出:
7
时间复杂度: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live