Pagini recente » Cod sursa (job #2239984) | Cod sursa (job #1641344) | Cod sursa (job #2211067) | Cod sursa (job #1870050) | Cod sursa (job #2147255)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin ("cerere.in" );
ofstream fout ("cerere.out");
const int N = 100001;
int a[N];
int n;
int v[N], c;
bool m[N];
void citire()
{
fin >> n;
int x, y;
for ( int i = 1 ; i <= n ; i++ )
{
fin >> v[i];
}
for ( int i = 1 ; i <= n ; i++ )
{
fin >> x >> y;
a[y] = x;
}
}
void dfs ( int x )
{
if ( v[x] != 0 )
{
int i = v[x] - 1, y = a[x];
while ( i > 0 )
{
y = a[y];
i--;
}
c++;
dfs(y);
}
}
int main()
{
citire();
for ( int i = 1 ; i <= n ; i ++ )
{
c = 0;
dfs(i);
fout << c << " ";
}
return 0;
}