Pagini recente » Cod sursa (job #1759242) | Cod sursa (job #1817574) | Cod sursa (job #1236763) | Cod sursa (job #69387) | Cod sursa (job #933083)
Cod sursa(job #933083)
#include<vector>
#include<fstream>
using namespace std;
ifstream cin("cerere.in");
ofstream cout("cerere.out");
const int N = 110000;
int n, k[N], s[N], nr, d[N];
vector<int> v[N];
void df(int nod) {
s[++nr] = nod;
if(k[nod])
d[nod] = d[s[nr - k[nod]]] + 1;
for(vector<int>::iterator it = v[nod].begin(); it != v[nod].end(); ++it)
df(*it);
--nr;
}
int main() {
int i;
cin >> n ;
for(i = 1; i <= n; ++i)
cin >> k[i];
for(i = 1; i < n;++i) {
int a, b;
cin >> a >> b;
v[a].push_back(b);
}
df(1);
for(i = 1; i <= n; ++i)
cout << d[i] << " ";
return 0;
}