Pagini recente » Cod sursa (job #1403140) | Cod sursa (job #2070222) | Cod sursa (job #442708) | Cod sursa (job #2942935) | Cod sursa (job #477915)
Cod sursa(job #477915)
#include <iostream>
#include <fstream>
#include <vector>
#define nmax 100005
using namespace std;
const char iname[] = "cerere.in";
const char oname[] = "cerere.out";
ifstream fin(iname);
ofstream fout(oname);
vector<int> A[nmax];
int sol[nmax], K[nmax];
bool ap[nmax], has_father[nmax];
int N, i, st[nmax], j, k, x, y, tot;
void DFS(int nod)
{
int i;
ap[nod] = true;
st[++tot] = nod;
if(K[nod] > 0)
sol[nod] = sol[ st[tot - K[nod] ] ] + 1;
for(i = 0; i < A[nod].size(); i ++)
if(ap[A[nod][i]] == false)
DFS(A[nod][i]);
--tot;
}
int main()
{
fin >> N;
for(i = 1; i <= N; i ++)
fin >> K[i];
for(i = 1; i <= N - 1; i ++)
{
fin >> x >> y;
A[x].push_back(y);
has_father[y] = true;
}
for(i = 1; i <= N; i ++)
{
tot = 0;
if(has_father[i] == false)
if(ap[i] == false)
DFS(i);
}
for(i = 1; i <= N; i ++)
fout << sol[i] << " ";
return 0;
}