Cod sursa(job #3159032)

Utilizator average_disappointmentManolache Sebastian average_disappointment Data 20 octombrie 2023 15:30:01
Problema Cerere Scor 100
Compilator cpp-64 Status done
Runda HLO 2023 - Cls 11-12 - Tema 0 Marime 0.87 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream cin("cerere.in");
ofstream cout("cerere.out");

const int nmax = 1e6;

int k[nmax + 1], ans[nmax + 1], index[nmax + 1];
bool has_parent[nmax + 1];

vector<int> children[nmax + 1];

void dfs(int curr_node, int depth = 0) {

	index[depth] = curr_node;
	ans[curr_node] = ans[index[depth - k[curr_node]]] + 1;

	for (auto child : children[curr_node])
		dfs(child, depth + 1);
}

int main() {

	int n;
	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> k[i];

	for (int i = 1; i <= n - 1; i++) {

		int a, b;
		cin >> a >> b;
		
		has_parent[b] = true;
		children[a].push_back(b);
	}

	int start_node;
	for (start_node = 1; start_node <= n; start_node++)
		if (!has_parent[start_node])
			break;

	dfs(start_node);

	for (int i = 1; i <= n; i++)
		cout << ans[i] - 1 << " ";
}