Cod sursa(job #683010)

Utilizator claudiumihailClaudiu Mihail claudiumihail Data 19 februarie 2012 20:45:10
Problema Cerere Scor 65
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <stack>

#define MAXN 100001

using namespace std;

typedef unsigned int uint32;
typedef vector<vector<int> > Graph;

int K[MAXN];
int vRequests[MAXN];
int vLevels[MAXN];

Graph graph;

void DFS(const int node, const int level)
{	
	for (uint32 i=0; i<graph[node].size(); ++i)
	{
		vLevels[level+1] = graph[node][i];
		
		if (K[graph[node][i]])
		{
			vRequests[graph[node][i]] = vRequests[vLevels[level+1-K[graph[node][i]]]]+1;
		}
		DFS(graph[node][i], level+1);
	}
}

int main()
{
	int n;
	vector<int> vRoots;
	fstream fin("cerere.in", fstream::in);
	fstream fout("cerere.out", fstream::out);
	
	fin >> n;
	//cout << n << "\n";
	
	graph.resize(n+1);
	
	for (int i=1; i<=n; ++i)
	{
		fin >> K[i];
		
		if (!K[i])
		{
			vRoots.push_back(i);
		}
	}
	
	for (int i=1; i<n; ++i)
	{
		int x,y;
		fin >> x >> y;
		
		graph[x].push_back(y);
	}
	
	for (uint32 i=0; i<vRoots.size(); ++i)
	{
		vLevels[0] = vRoots[i];

		DFS(vRoots[i], 0);
	}
	
	for (int i=1; i<=n; ++i)
	{
		fout << vRequests[i] << " ";
	}
	fout << "\n";
	
	fin.close();
	fout.close();
	return 0;
}