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.

75 lines
2.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
#define re register
typedef long long ll;
using namespace std;
inline ll read() {
ll a = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
a = a * 10 + c - '0';
c = getchar();
}
return a * f;
} // 好用的快读
ll n, q;
ll head[200010], dp[200010][21];
struct ljj {
ll to, stb;
} a[200010];
ll s = 0;
inline void insert(ll x, ll y) {
s++;
a[s].stb = head[x];
a[s].to = y;
head[x] = s;
}
inline void dfs(ll x, ll fa) {
for (re ll i = head[x]; i; i = a[i].stb) {
ll xx = a[i].to;
if (xx == fa)
continue;
dfs(xx, x);
for (re ll j = 1; j <= q; j++)
dp[x][j] += dp[xx][j - 1]; // 第一遍dp
}
}
inline void dfs1(ll x, ll fa) {
for (re ll i = head[x]; i; i = a[i].stb) {
ll xx = a[i].to;
if (xx == fa)
continue;
// 在第一次遍历时 dp[1][2] 包括了 dp[2][1] 2的子树权值
// 然鹅 ans在统计dp[2][3] 的时候也加上了 dp[2][1] 2的子树权值
// 第二次遍历 dp[2][3] 又加上了 dp[2][1];
// 所以需要简单容斥一下;
for (re ll j = q; j >= 2; j--)
dp[xx][j] -= dp[xx][j - 2]; // 简单容斥
for (re ll j = 1; j <= q; j++)
dp[xx][j] += dp[x][j - 1]; // 第二遍dp
dfs1(xx, x);
}
}
int main() {
n = read();
q = read();
for (re ll i = 1; i < n; i++) {
ll x = read(), y = read();
insert(x, y);
insert(y, x);
}
for (re ll i = 1; i <= n; i++)
dp[i][0] = read(); // 每个节点往外0距离就是它本身的权值
dfs(1, 0);
dfs1(1, 0);
for (re ll i = 1; i <= n; i++) {
ll ans = 0;
for (re ll j = 0; j <= q; j++)
ans += dp[i][j]; // ans统计答案
printf("%lld\n", ans);
}
return 0;
}