### 正解

```#include <bits/stdc++.h>
#define N 100005

using namespace std;

int n, k;
int o[N], d[N], a[N], totVec;
int fa[N], wson[N], dist[N];
int head[N], nex[N << 1], to[N << 1], ecnt;

long long ans[N];
vector<long long> f[N], g[N];

int x = 0; char ch = getchar();
while(!isdigit(ch)) ch = getchar();
while(isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return x;
}
inline void addE(int u, int v) {
to[++ecnt] = v;
}

void preDfs(int u) {
for(int i = head[u], v; i; i = nex[i]) {
v = to[i];
if(v == fa[u]) continue;
fa[v] = u, preDfs(v);
if(dist[wson[u]] < dist[v])
wson[u] = v;
}
dist[u] = dist[wson[u]] + 1;
}

void dfs(int u, int top) {
if(u == top) {
o[u] = ++totVec;
f[o[u]].resize(dist[u]);
g[o[u]].resize(dist[u]);
}
if(wson[u]) {
o[wson[u]] = o[u];
d[wson[u]] = d[u] + 1;
dfs(wson[u], top);
}

++f[o[u]][d[u]];
g[o[u]][d[u]] += a[u];
for(int i = head[u], v; i; i = nex[i]) {
v = to[i];
if(v == fa[u] || v == wson[u]) continue;
dfs(v, v);
for(int j = 0; j < dist[v]; ++j)
if(k - j - 1 > 0 && k - j - 1 + d[u] < dist[top]) {
ans[u] += f[o[u]][k - j - 1 + d[u]] * g[o[v]][j];
ans[u] += g[o[u]][k - j - 1 + d[u]] * f[o[v]][j];
}
for(int j = 0; j < dist[v]; ++j) {
f[o[u]][j + 1 + d[u]] += f[o[v]][j];
g[o[u]][j + 1 + d[u]] += g[o[v]][j];
}
}
}

int main() {
for(int i = 1; i <= n; ++i)
for(int i = 1, u, v; i < n; ++i) {
}

preDfs(1);
dfs(1, 1);

for(int i = 1; i <= n; ++i)
printf("%lld%c", ans[i], " \n"[i == n]);
return 0;
}```