Pagini recente » Cod sursa (job #303474) | Cod sursa (job #351473) | Cod sursa (job #38404) | Cod sursa (job #379157) | Cod sursa (job #2778872)
#include <bits/stdc++.h>
#define DIM 100005
using namespace std;
ifstream f("cerere.in");
ofstream g("cerere.out");
int n, K[DIM], G[DIM], root, node[DIM];
bitset <DIM> v;
vector <int> edges[DIM];
void dfs(int nod, int niv)
{
node[niv] = nod;
G[nod] = G[node[niv - K[nod]]] + 1;
for(auto child : edges[nod])
dfs(child, niv + 1);
}
int main()
{
f >> n;
for(int i = 1; i <= n; i++)
f >> K[i];
for(int i = 1; i < n; i++)
{
int x, y;
f >> x >> y;
edges[x].push_back(y);
v[y] = 1;
}
for(int i = 1; i <= n; i++)
if(!v[i])
{
root = i;
break;
}
dfs(root, 0);
for(int i = 1; i <= n; i++)
g << G[i] - 1 << " ";
return 0;
}