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.
30 lines
680 B
30 lines
680 B
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
const int N = 100010;
|
|
vector<int> g[N];
|
|
int c[N];
|
|
int dfs(int u, int fa, int k) {
|
|
if (k == 0) return c[u];
|
|
int sum = c[u];
|
|
for (int i = 0; i < g[u].size(); i++) {
|
|
if (g[u][i] == fa) continue;
|
|
sum += dfs(g[u][i], u, k - 1);
|
|
}
|
|
return sum;
|
|
}
|
|
int main() {
|
|
int n;
|
|
cin >> n;
|
|
for (int i = 1; i <= n; i++) cin >> c[i];
|
|
for (int i = 1; i < n; i++){
|
|
int a, b;
|
|
cin >> a >> b;
|
|
g[a].push_back(b);
|
|
g[b].push_back(a);
|
|
}
|
|
int k;
|
|
cin >> k;
|
|
for (int i = 1; i <= n; i++)
|
|
cout << dfs(i, -1, k) << endl;
|
|
return 0;
|
|
} |