Pagini recente » Cod sursa (job #2951413) | Cod sursa (job #883572) | Cod sursa (job #985041) | Cod sursa (job #1448631) | Cod sursa (job #2958943)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cerere.in");
ofstream fout("cerere.out");
int n, ancestor[100005], viz[100005], dp[100005], level[100005];
vector <int> a[100005];
void Dfs(int x, int niv, int last)
{
level[niv] = x;
if (ancestor[x])
dp[x] = 1 + dp[level[niv - ancestor[x]]];
for (auto w : a[x])
if (w != last)
Dfs(w, niv + 1, x);
}
int main()
{
int i, x, y;
fin >> n;
for (i = 1; i <= n; i++)
fin >> ancestor[i];
for (i = 1; i <= n; i++)
{
fin >> x >> y;
a[x].push_back(y);
viz[y] = 1;
}
for (i = 1; i <= n; i++)
if (viz[i] == 0)
{
Dfs(i, 1, 0); /// root
break;
}
for (i = 1; i <= n; i++)
fout << dp[i] << " ";
return 0;
}