Pagini recente » Cod sursa (job #246123) | Cod sursa (job #2393673) | Cod sursa (job #1671220) | Cod sursa (job #598629) | Cod sursa (job #1148011)
#include <cstdio>
#include <cmath>
#include <cstring>
#define Nmax 100005
using namespace std;
int cere[Nmax],N;
int DP[18][Nmax];
void read()
{
int a,b;
scanf("%d",&N);
for(int i = 1; i <= N; ++i) scanf("%d",&cere[i]);
for(int i = 1; i < N; ++i)
{
scanf("%d%d",&a,&b);
DP[0][b] = a;
}
}
void precompute()
{
int len = (int)log2(N);
for(int i = 1; i <= len; ++i)
for(int j = 1; j <= N; ++j)
DP[i][j] = DP[i-1][DP[i-1][j]];
}
int memo[Nmax];
void solve()
{
memset(memo,-1,sizeof(memo));
int x,total,put,val,i;
for(i = 1; i <= N; ++i)
{
total = 0;
put = 0;
x = i;
while(cere[x])
{
++total;
val = cere[x];
while(val)
{
while(1<<put <= val)++put;
--put;
x = DP[put][x];
val -= 1<<put;
}
if(memo[x] != -1)
{
total += memo[x];
break;
}
}
memo[i] = total;
printf("%d ",total);
}
}
int main()
{
freopen("cerere.in","r",stdin);
freopen("cerere.out","w",stdout);
read();
precompute();
///solve();
return 0;
}