Pagini recente » Cod sursa (job #1655695) | Cod sursa (job #2512342) | Cod sursa (job #2410601) | Cod sursa (job #1944270) | Cod sursa (job #1864750)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cerere.in");
ofstream fout("cerere.out");
const int NMAX = 100005;
int n,x,y;
int kcost[NMAX],lev[NMAX];
vector<int> G[NMAX];
int dp[20][NMAX];
void DFS(int,int);
int getParent(int,int);
void DFS(int n,int l)
{
lev[n]=l;
for(auto w:G[n])
DFS(w,l+1);
}
int getParent(int nod,int l)
{
for(int p=0 ; (1<<p)<=l ; p++)
if(l&(1<<p))
nod=dp[p][nod];
if(kcost[nod]) return getParent(nod,kcost[nod]);
return nod;
}
int main()
{
fin>>n;
for(int i=1;i<=n;i+=1) fin>>kcost[i];
for(int i=1;i<n;i+=1)
{
fin>>x>>y;
dp[0][y]=x;
G[x].push_back(y);
}
for(int p=1; (1<<p)<=n ; p++)
for(int i=1; i<=n; i++)
dp[p][i]=dp[p-1][dp[p-1][i]];
DFS(1,0);
for(int i=1;i<=n;i+=1)
{
int r = getParent(i,kcost[i]);
if( abs(lev[i]-lev[r])<=1 ) fout<<abs(lev[i]-lev[r]);
else fout<<abs(lev[i]-lev[r])-1;
fout << ' ';
}
return 0;
}