You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.2 KiB

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
vector<int> edge[N];
int f[N][25], g[N][25];
int val[N];
int n, k;
void dp(int u, int fa) {
for (int i = 0; i <= k; i++)
f[u][i] = val[u];
for (int i = 0; i < edge[u].size(); i++) {
int son = edge[u][i];
if (son == fa) continue;
dp(son, u);
for (int j = 1; j <= k; j++) f[u][j] += f[son][j - 1];
}
return;
}
void dp2(int u, int fa) {
for (int i = 0; i < edge[u].size(); i++) {
int son = edge[u][i];
if (son == fa)
continue;
g[son][0] = val[son];
g[son][1] = f[son][1] + val[u];
for (int j = 2; j <= k; j++) {
g[son][j] = g[u][j - 1] + f[son][j] - f[son][j - 2];
}
dp2(son, u);
}
}
int main() {
cin >> n >> k;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
edge[a].push_back(b);
edge[b].push_back(a);
}
for (int i = 1; i <= n; i++) cin >> val[i];
dp(1, 0);
for (int i = 0; i <= k; i++) g[1][i] = f[1][i];
dp2(1, 0);
for (int i = 1; i <= n; i++) cout << g[i][k] << endl;
return 0;
}