Pagini recente » Cod sursa (job #1598533) | Cod sursa (job #2703896) | Cod sursa (job #2328640) | Monitorul de evaluare | Cod sursa (job #3158047)
#include <fstream>
#include <iostream>
using namespace std;
ifstream fin ("cerere.in");
ofstream fout("cerere.out");
int n, k[100000], v[100000], a, b;
void citire()
{
fin >> n;
for (int i = 1 ; i <= n ; ++i)
fin >> k[i];
for (int i = 1 ; i < n ; ++i)
{
fin >> a >> b;
v[b] = a;
}
}
int main()
{
citire();
for (int i = 1 ; i <= n ; ++i)
{
if (!k[i])
fout << 0 << " ";
else
{
bool gasit = false;
int c = 0;
int x = v[i];
int y = k[i];
while (!gasit)
{
for (int j = 1 ; j < y ; ++j)
x = v[x];
++c;
if (!k[x])
gasit = true;
else
{
x = v[x];
y = k[x];
}
}
fout << c << " ";
}
}
return 0;
}