Pagini recente » Cod sursa (job #2932102) | Cod sursa (job #3151727) | Cod sursa (job #2492388) | Cod sursa (job #1649610) | Cod sursa (job #1068551)
#include <fstream>
#include <vector>
using namespace std;
ifstream f("cerere.in");
ofstream g("cerere.out");
const int MAX_N = 100005;
int n, Stack[MAX_N], K[MAX_N], ans[MAX_N], radacina;
bool used[MAX_N], notRoot[MAX_N];
vector <int> L[MAX_N];
void read() {
f >> n;
for (int i = 1; i <= n; i++)
f >> K[i];
for (int i = 1; i <= n - 1; i++) {
int a, b;
f >> a >> b;
L[a].push_back (b);
notRoot[b] = 1;
}
for (int i = 1; i <= n; i++)
if (!notRoot[i])
radacina = i;
}
void dfs (int nod, int depth) {
used[nod] = 1;
Stack[depth] = nod;
ans[nod] += ans[Stack[depth - K[nod]]] + 1;
for (size_t i = 0; i < L[nod].size(); i++) {
int fiu = L[nod][i];
if (!used[fiu])
dfs (fiu, depth + 1);
}
}
int main() {
read();
dfs (radacina, 0);
for (int i = 1; i <= n; i++)
g << ans[i] - 1 << " ";
return 0;
}