Pagini recente » Cod sursa (job #2498970) | Cod sursa (job #2169583) | Cod sursa (job #1034763) | Cod sursa (job #1182003) | Cod sursa (job #863755)
Cod sursa(job #863755)
#include <iostream>
#include <fstream>
#include <vector>
#define NMAX 100010
using namespace std;
int n;
int v[NMAX],sol[NMAX],tat[NMAX];
bool viz[NMAX];
vector <int> G[NMAX];
vector <int> Q;
void DFS(int nod)
{
viz[nod]=true;
Q.push_back(nod);
if(v[nod] ==0)sol[nod]=0;
else
sol[nod]=sol[Q[Q.size()-1-v[nod]]]+1;
for(int i=0;i<G[nod].size();i++)
if(!viz[G[nod][i]]) DFS(G[nod][i]);
Q.pop_back();
}
int main()
{
ifstream fin("cerere.in");
ofstream fout("cerere.out");
fin>>n;
for(int i=1;i<=n;i++)
fin>>v[i];
int t,f;
for(int i=1;i<=n-1;i++)
{
fin>>t>>f;
tat[f]=t;
G[t].push_back(f);
}
sol[0]=-1;
int s=1;
while(tat[s]!=0)
s=tat[s];
DFS(s);
for(int i=1;i<=n;i++)
fout<<sol[i]<<" ";
return 0;
}