Cod sursa(job #2310626)

Utilizator mihai50000Mihai-Cristian Popescu mihai50000 Data 1 ianuarie 2019 18:46:31
Problema Cerere Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.7 kb
#include <bits/stdc++.h>
using namespace std;

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

const int DIM = 1e5 + 7;

bool can[DIM];
int c[DIM];
int nr[DIM];

vector <int> v[DIM];
vector <int> stiva;

void dfs(int nod)
{
	if(c[nod] == 0)
		nr[nod] = 0;
	else
		nr[nod] = nr[stiva[stiva.size() - c[nod]]] + 1;
	
	stiva.push_back(nod);
	
	for(auto i : v[nod])
		dfs(i);
	
	stiva.pop_back();
}

int main()
{
	int n;
	in >> n;
	
	for(int i = 1; i <= n; i++)
		in >> c[i];
	
	for(int i = 1; i < n; i++)
	{
		int x, y;
		in >> x >> y;
		
		v[x].push_back(y);
		
		can[y] = 1;
	}
	
	for(int i = 1; i <= n; i++)
		if(can[i] == 0)
		{
			dfs(i);
			break;
		}
	
	for(int i = 1; i <= n; i++)
		out << nr[i] << ' ';
 }