-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseek.cpp
More file actions
71 lines (65 loc) · 1.4 KB
/
Copy pathseek.cpp
File metadata and controls
71 lines (65 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
const int MAXN = 105;
int n, m;
int fa[MAXN] = {0};
int nodev[MAXN];
int dp[MAXN][MAXN] = {0};
vector<int> to[MAXN];
void dfs(int x){
dp[x][1] = nodev[x];
for(int i=0; i<to[x].size(); i++){
int y = to[x][i];
if(fa[x] == y) continue;
fa[y] = x;
dfs(y);
for(int j=m-1; j>0; j--){
for(int k=1; k<j; k++){
dp[x][j+1] = max(dp[x][j+1], dp[x][k]+dp[y][j-k]);
}
}
}
for(int j=m-1; j>0; j--){
dp[x][j+1] = dp[x][j] + nodev[x];
}
for(int i=0; i<to[x].size(); i++){
int y = to[x][i];
if(fa[x] == y) continue;
for(int j=1; j<=m; j++){
dp[x][j] = max(dp[x][j], dp[y][j]);
}
}
return;
}
int main(){
int tf, tt;
scanf("%d%d", &n, &m);
for(int i=1; i<=n; i++){
scanf("%d", &nodev[i]);
}
for(int i=1; i<n; i++){
scanf("%d%d", &tf, &tt);
to[tf].push_back(tt);
to[tt].push_back(tf);
}
dfs(1);
int gans = 0;
/**
for(int i=1; i<=n;i++){
cout << i << "-> ";
for(int j=1; j<=m; j++){
cout << dp[i][j] << " ";
}
cout << endl;
}
**/
for(int i=1; i<=m; i++){
gans = max(gans, dp[1][i]);
}
printf("%d", gans);
return 0;
}