Pagini recente » Cod sursa (job #79711) | Cod sursa (job #2645870) | Cod sursa (job #1334220) | Cod sursa (job #2498150) | Cod sursa (job #2710154)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cerere.in");
ofstream fout("cerere.out");
const int nmax = 100005;
int n, k[nmax], radacina[nmax], cnt[nmax], fr[nmax];
vector <int> G[nmax];
void dfs(int nod, int nivel){
if (k[nod] != 0){
cnt[nod] = 1 + cnt[fr[nivel - k[nod]]];
}
fr[nivel] = nod;
for (auto it : G[nod]){
dfs(it, nivel + 1);
}
}
int main(){
fin >> n;
for (int i = 1; i <= n; ++i){
fin >> k[i];
}
for (int i = 1; i < n; ++i){
int x, y;
fin >> x >> y;
radacina[y] = 1;
G[x].push_back(y);
}
int root;
for (int i = 1; i <= n; ++i){
if (radacina[i] == 0){
root = i;
break;
}
}
dfs(root, 0);
for (int i = 1; i <= n; ++i){
fout << cnt[i] << " ";
}
return 0;
}