Pagini recente » Cod sursa (job #48273) | Cod sursa (job #2924556) | Cod sursa (job #807206) | Cod sursa (job #2573394) | Cod sursa (job #1041093)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
int up[100005], dad[100005], ans[100005];
vector<int> graph[100005];
vector<int> st;
void dfs(int x){
st.push_back(x);
if(up[x] == 0)
ans[x] = 0;
else
ans[x] = ans[st[st.size() - 1 - up[x]]] + 1;
for(int i = 0; i < graph[x].size(); ++i)
dfs(graph[x][i]);
st.pop_back();
}
int main(){
ifstream in("cerere.in");
ofstream out("cerere.out");
int n;
in >> n;
for(int i = 1; i <= n; ++i)
in >> up[i];
for(int i = 1; i < n; ++i){
int x, y;
in >> x >> y;
graph[x].push_back(y);
dad[y] = x;
}
for(int i = 1; i <= n; ++i)
if(dad[i] == 0)
dfs(i);
for(int i = 1; i <= n; ++i)
out << ans[i] << " ";
return 0;
}