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.

78 lines
1.6 KiB

#include <bits/stdc++.h>
#define endl '\n'
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
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 father) {
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 == father)
continue;
dp(son, u);
for (int j = 1; j <= k; j++) {
f[u][j] += f[son][j - 1];
}
}
return;
}
void dp2(int u, int father) {
for (int i = 0; i < edge[u].size(); i++) {
int son = edge[u][i];
if (son == father)
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);
}
}
void solve() {
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 << f[i][k] << endl;
// }
// cout << endl;
for (int i = 1; i <= n; i++) {
cout << g[i][k] << endl;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
}