Pagini recente » Cod sursa (job #620621) | Cod sursa (job #697560) | Cod sursa (job #1809444) | Cod sursa (job #952661) | Cod sursa (job #2147361)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin ("cerere.in" );
ofstream fout ("cerere.out");
const int N = 100001;
int a[N], d[N];
vector <int> o[N];
int n;
int v[N], c, rez[N];
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;
o[x].push_back(y);
o[y].push_back(x);
}
}
void dfs ( int x , int nivel )
{
m[x] = 1;
d[nivel] = x;
rez[x] = rez[ d[nivel - v[x]]] + 1;
int y;
for ( int i = 0 ; i < o[x].size() ; i++ )
{
y = o[x][i];
if ( !m[y] )
{
if ( a[x] != y )
{
dfs(y, nivel+1);
}
}
}
}
int main()
{
citire();
int f = 1;
while ( a[f] && f <= n)
{
f++;
}
dfs(f,0);
for ( int i = 1 ; i <= n ; i++ )
{
fout << rez[i] - 1 << " ";
}
return 0;
}