Pagini recente » Cod sursa (job #68066) | Cod sursa (job #1256369) | Cod sursa (job #1963348) | Cod sursa (job #1873364) | Cod sursa (job #1853104)
#include <fstream>
#include <vector>
#include <cstring>
#define hg 1<<10
#define verf ++poz == hg ? fin.read (ch, hg), poz = 0 : 0
#define nmax 100001
using namespace std;
ifstream fin("cerere.in");
ofstream fout("cerere.out");
int n,v[nmax],n1,stiva[nmax],k[nmax],vf,poz;
bool viz[nmax];
char ch[hg];
vector < int > leg[nmax];
inline void cit( int &x ) {
if (ch[0] == '\0') fin.read (ch, hg) ;
else for (; ch[poz] < '0' || ch[poz] > '9' ; verf) ;
for (x = 0 ; ch[poz] >= '0' && ch[poz] <= '9' ; x = x * 10 + ch[poz] - '0', verf) ;
}
inline void read()
{
int i,a,b;
cit(n);
for(i=1; i<=n; i++)
cit(v[i]);
for(i=1; i<n; i++)
{
cit(a);cit(b);
viz[b]=true;
leg[a].push_back(b);
}
fin.close();
for(i=1; i<=n; i++)
if(viz[i]==false)
{
n1=i;
break;
}
if(v[n1])
v[n1]=0;
}
void DFS(int x)
{
int i;
viz[x]=true;
if(v[x])
k[x]=stiva[vf-v[x]+1];
stiva[++vf]=x;
for(i=0; i<leg[x].size(); i++)
{
if(!viz[leg[x][i]])
{
DFS(leg[x][i]);
while(stiva[vf]!=x)
vf--;
}
}
}
int no(int x)
{
if(!k[x])
return 1;
else
return 1+no(k[x]);
}
inline void write()
{
int nr,x;
for(int i=1; i<=n; i++)
{
if(!v[i])
fout<<"0 ";
else
fout<<no(k[i])<<' ';
}
fout.close();
}
int main()
{
read();
memset(viz, false, nmax);
DFS(n1);
write();
return 0;
}