Pagini recente » Cod sursa (job #2272211) | Cod sursa (job #1975330) | Cod sursa (job #223803) | Cod sursa (job #351219) | Cod sursa (job #2135515)
#include <fstream>
#include <vector>
using namespace std;
ifstream f("cerere.in");
ofstream g("cerere.out");
const int nmax = 100005;
int n, i, j, stm[nmax], x, y, cnt[nmax], stv[nmax], vf;
vector<int> ls[nmax];
void dfs(int x) {
stv[++vf] = x;
if (stm[x] > 0 && stm[x] < vf) cnt[x] += cnt[stv[vf-stm[x]]] + 1;
for (auto y : ls[x])
dfs(y);
vf--;
}
int main() {
f >> n;
for (i = 1; i <= n; i++)
f >> stm[i];
for (i = 1; i < n; i++) {
f >> x >> y;
ls[x].push_back(y);
}
dfs(1);
for (i = 1; i <= n; i++) g << cnt[i] << ' ';
return 0;
}