Cod sursa(job #1245220)

Utilizator cioionutFMI Ionut Ciocoiu cioionut Data 18 octombrie 2014 19:24:25
Problema Cerere Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include<iostream>
#include<fstream>
#include<vector>
#include<queue>
using namespace std;
int main()
{
	ifstream f("cerere.in");
	ofstream g("cerere.out");
	int n,i,j;
	f >> n;
	int *a = new int[n+1], *t = new int[n+1],*d=new int[n+1];
	vector <int> v[100001];
	for (i = 1; i <= n; i++) {
		f >> a[i]; 
	}
	t[1] = 0;
	while (!f.eof()) {
		f >> i >> j;
		v[i].push_back(j);
		t[j] = i;
	}
	for (i = 1; i <= n;i++)
	if (a[i] == 0) d[i] = 0;
	else {
		d[i] = i;
		for (j = 1; j <= a[i]; j++) d[i] = t[d[i]];
	}
	
	queue <int> q;
	q.push(1);
	while (!q.empty()) {
		if (d[q.front()] != 0)  d[q.front()]=d[d[q.front()]]+1;
		for (i = 0; i < v[q.front()].size(); i++)
			q.push(v[q.front()][i]);
		q.pop();
	}
	for (i = 1; i <= n; i++) g << d[i] << " ";
	//cin.get();
	f.close();
	g.close();
	return 0;
}