Pagini recente » Cod sursa (job #3341208) | Cod sursa (job #3349289) | Cod sursa (job #3345556) | Cod sursa (job #3354997) | Cod sursa (job #3317429)
#include <bits/stdc++.h>
std::ifstream fin("cerere.in");
std::ofstream fout("cerere.out");
const int MOD = 1e9 + 7;
const int INF = 2e9;
const int64_t LONG_INF = static_cast<int64_t>(1e18);
const int NMAX = 16e3 + 5;
int n, m, x;
std::vector<int> graph[NMAX];
int father[NMAX], value[NMAX];
int lvl_node[NMAX], answer[NMAX];
void dfs(int node, int level)
{
lvl_node[level] = node;
if(value[node] == 0)
answer[node] = 0;
else
answer[node] = answer[lvl_node[level - value[node]]] + 1;
for(auto adj : graph[node])
dfs(adj, level + 1);
}
int main()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
fin >> n;
for(int i = 1; i <= n; ++i)
fin >> value[i];
for(int i = 1; i < n; ++i)
{
int x, y;
fin >> x >> y;
graph[x].push_back(y);
father[y] = x;
}
int root = 1;
for(int i = 1; i <= n; ++i)
if(!father[i])
{
root = i;
break;
}
dfs(root, 1);
for(int i = 1; i <= n; ++i)
fout << answer[i] << " ";
return 0;
}