Topic: https://vjudge.net/contest/323605#problem/A
The meaning of problems: a tree that lets you select a block of m-point communication, so that the maximum weights obtained
Thinking: tree backpack, we use an array dp, dp [i] [j], which is selected from the maximum value of the j-th sub-tree nodes representative obtained when i is root, then we are in our roots for each i is as there are m items, and as a different grouping for different subtrees can
#include<bits/stdc++.h> #define maxn 105 #define mod 1000000007 using namespace std; typedef long long ll; int n,m; int dp[maxn][maxn]; int d[maxn]; vector<int> mp[maxn]; void dfs(int x,int f){ for(int i=0;i<mp[x].size();i++){ int u=mp[x][i]; if(u==f) continue; DFS (U, X); D [X];for ( int T = m; T> = 0 ; T-- ) {// this sub-tree with the obtained article updating the value of the current node for ( int J = T; J> = 0 ; J, ) { IF (TJ> = 0 ) { DP [X] [T] = max (DP [X] [T], DP [X] [TJ] + DP [U] [J]); } } } } IF (X! = 0 ) { for ( int T = m; T> 0 ; T-- ) { DP [X] [T] = DP [X] [T- . 1 ] + } } } int main(){ scanf("%d%d",&n,&m); int x,y; for(int i=1;i<=n;i++) scanf("%d",&d[i]); for(int i=0;i<n-1;i++){ scanf("%d%d",&x,&y); mp[x].push_back(y); mp[y].push_back(x); } dfs(1,-1); cout<<dp[1][m]; }