Cod sursa(job #477915)

Utilizator dornescuvladVlad Eugen Dornescu dornescuvlad Data 16 august 2010 16:49:07
Problema Cerere Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <iostream>
#include <fstream>
#include <vector>
#define nmax 100005 

using namespace std;

const char iname[] = "cerere.in";
const char oname[] = "cerere.out";

ifstream fin(iname);
ofstream fout(oname);

vector<int> A[nmax];
int sol[nmax], K[nmax];
bool ap[nmax], has_father[nmax];
int N, i, st[nmax], j, k, x, y, tot;

void DFS(int nod)
{	
	int i;
	ap[nod] = true;
	st[++tot] = nod;
	if(K[nod] > 0)
		sol[nod] = sol[ st[tot - K[nod] ] ] + 1;
	for(i = 0; i < A[nod].size(); i ++)
		if(ap[A[nod][i]] == false)
			DFS(A[nod][i]);
	--tot;
}
		
int main()
{
	fin >> N;
	for(i = 1; i <= N; i ++)
		fin >> K[i];
	for(i = 1; i <= N - 1; i ++)
	{
		fin >> x >> y;
		A[x].push_back(y);
		has_father[y] = true;
	}
		
	for(i = 1; i <= N; i ++)
	{	
		tot = 0;
		if(has_father[i] == false)
			if(ap[i] == false)
				DFS(i);
	}
	for(i = 1; i <= N; i ++)
		fout << sol[i] << " ";
	return 0;
}