Pagini recente » Cod sursa (job #259053) | Cod sursa (job #1941962) | Cod sursa (job #1043627) | Cod sursa (job #746478) | Cod sursa (job #3159730)
//oftica si durere in suflet
#include <fstream>
#include <cmath>
#include <algorithm>
#define ll long long
using namespace std;
const int NMAX = 1e5;
const int LGMAX = 18;
int d[NMAX + 1][LGMAX + 1];
int lg[NMAX + 1];
int cat[NMAX + 1];
int jump(int nod, int pas)
{
int i, ans = nod;
for (i = 0; (1 << i) <= pas; i++)
if (pas & (1 << i))
ans = d[ans][i];
return ans;
}
int nxt[NMAX + 1][LGMAX + 1];
int cnt[NMAX + 1][LGMAX + 1];
signed main()
{
ifstream cin("cerere.in");
ofstream cout("cerere.out");
int n, i, j;
cin >> n;
for (i = 1; i <= n; i++)
{
cin >> cat[i];
}
for (i = 2; i <= n; i++)
{
int a, b;
cin >> a >> b;
d[b][0] = a;
lg[i] = lg[i / 2] + 1;
}
for (i = 1; i <= lg[n]; i++)
for (j = 1; j <= n; j++)
d[j][i] = d[d[j][i - 1]][i - 1];
for (i = 1; i <= n; i++)
{
nxt[i][0] = jump(i, cat[i]);
cnt[i][0] = (nxt[i][0] != i);
}
for (i = 1; i <= lg[n]; i++)
for (j = 1; j <= n; j++)
{
nxt[j][i] = nxt[nxt[j][i - 1]][i - 1];
if (nxt[j][i] != nxt[j][i - 1])
cnt[j][i] = cnt[j][i - 1] + cnt[nxt[j][i - 1]][i - 1];
else
cnt[j][i] = cnt[j][i - 1];
}
for (i = 1; i <= n; i++)
{
cout << cnt[i][lg[n]] << " ";
}
}