Cod sursa(job #2502182)

Utilizator _Tudor_Tudor C _Tudor_ Data 30 noiembrie 2019 13:38:19
Problema Cerere Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <bits/stdc++.h>
#pragma warning(disable : 4996)

using namespace std;

ifstream fin("cerere.in");
ofstream fout("cerere.out");

const int NMax = 100000;
int n;
bool use[NMax + 5];
int k[NMax + 5];
int sol[NMax + 5];
vector<int> g[NMax+ 5];

int st[NMax + 5];

void read()
{
	fin >> n;
	for(int i = 1; i <= n; i++)
		fin >> k[i];

	int x, y;
	while (fin >> x >> y)
	{
		g[x].push_back(y);
		//g[y].push_back(x);
	}
}

void DFS(int nod)
{
	use[nod] = true;
	st[++st[0]] = nod;
    if(k[nod] == 0)
		sol[nod] = 0;
	else
		sol[nod] = sol[st[st[0]- k[nod]]] + 1;

	for (auto vecin : g[nod]) 
	{
		if(!use[vecin])
			DFS(vecin);
	}
	st[0]--;
}

int main()
{
	read();
	
	DFS(1);

	for(int i = 1; i <= n; i++)
		fout << sol[i] << ' ';
}