Pagini recente » Cod sursa (job #128506) | Cod sursa (job #819555) | Cod sursa (job #1360281) | Cod sursa (job #1052778) | Cod sursa (job #2770215)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cerere.in");
ofstream fout("cerere.out");
int n,i,x,y,T[100010],K[100010],sol[100010],f[100010],v[100010],k;
vector<int> L[100010];
void dfs(int nod){
f[nod]=1;
v[++k]=nod;
for(int i=0;i<L[nod].size();i++){
int vec=L[nod][i];
if(!f[vec]){
if(K[vec])
sol[vec]=sol[v[k-K[vec]+1]]+1;
K[vec]=0;
dfs(vec);
k--;
}
}
}
int main(){
fin>>n;
for(i=1;i<=n;i++)
fin>>K[i];
for(i=1;i<n;i++){
fin>>x>>y;
L[x].push_back(y);
L[y].push_back(x);
T[y]=x;
}
for(i=1;i<=n;i++)
if(!T[i]&&!K[i])
break;
dfs(i);
for(i=1;i<=n;i++)
fout<<sol[i]<<" ";
return 0;
}