Pagini recente » Cod sursa (job #2840670) | Cod sursa (job #1513538) | Cod sursa (job #2591882) | Cod sursa (job #3207538) | Cod sursa (job #3180691)
#include <fstream>
#include <vector>
#define N 100002
std::ifstream fin("cerere.in");
std::ofstream fout("cerere.out");
int ans[N], parent[N], viz[N], Q[N], LogN, n, root[N];
std::vector<int> path;
std::vector<std::vector<int>> tree;
void DFS(int node, int h){
viz[node] = true;
path.push_back(node);
ans[node] = ans[path[h - Q[node]]] + 1;
for(int it : tree[node]){
if(!viz[it]){
DFS(it, h + 1);
}
}
path.pop_back();
}
int main(){
fin >> n;
tree = std::vector<std::vector<int>> (n + 1);
while((1 << LogN) <= n){
LogN ++;
}
for(int i = 1; i <= n; i++){
fin >> Q[i];
}
int x, y;
for(int i = 1; i <= n - 1; i++){
fin >> x >> y; root[y] = 1;
tree[x].push_back(y);
tree[y].push_back(x);
}
for(int i = 1; i <= n; i++){
if(!root[i]){
DFS(i, 0);
break;
}
}
for(int i = 1; i <= n; i++)
fout << ans[i] - 1 << " ";
}