You are given a tree, which consists of 𝑛 vertices. Recall that a tree is a connected undirected graph without cycles.

Example of a tree.
Vertices are numbered from 1 to 𝑛. All vertices have weights, the weight of the vertex 𝑣 is 𝑎𝑣.

Recall that the distance between two vertices in the tree is the number of edges on a simple path between them.

Your task is to find the subset of vertices with the maximum total weight (the weight of the subset is the sum of weights of all vertices in it) such that there is no pair of vertices with the distance 𝑘 or less between them in this subset.

链接🔗

「CodeForces 1249E」Maximum Weight Subset

题解

DP[i][j] 表示以i为根,最小选j层的最大权值
考虑每个节点选不选,如果选则更新DP[i][0]
不选则枚举距离层次j,依次选取子儿子进行更新答案,时间复杂度为O(N3)O(N^3)

代码

#include <cstring>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 205;
 
int n, k;
vector<int> G[maxn];
int dp[maxn][maxn], a[maxn];
 
void dfs(int x, int f = 0)
{
    dp[x][0] = a[x];
    for(int v:G[x])
    {
        if(v == f) continue;
        dfs(v, x);
        dp[x][0] += dp[v][k];
    }
    for(int i = 1; i < n; i++)
    {
        for(auto it : G[x])
        {
            if(it == f) continue;
            int cnt = dp[it][i-1];
            for(auto other:G[x])
            {
                if(other == it || other == f) continue;
                cnt += dp[other][max(i-1,k-i)];
            }
            dp[x][i] = max(dp[x][i],cnt);
        }
    }
    for(int i = n-1; i >= 0; i--)
        dp[x][i-1] = max(dp[x][i-1], dp[x][i]);
}
int main()
{
//    freopen("a.in","r",stdin);
//    freopen("k.out","w",stdout);
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for(int i = 1; i < n; i++)
    {
        int a,b;
        scanf("%d%d", &a, &b);
        G[a].push_back(b);G[b].push_back(a);
    }
    dfs(1);
    printf("%d\n", dp[1][0]);
    return 0;
}