Cod sursa(job #670686)

Utilizator ELHoriaHoria Cretescu ELHoria Data 29 ianuarie 2012 19:42:29
Problema Mese Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.73 kb
#include <cstdio>
#include <vector>

using namespace std;

const int NMAX = 100005;
vector<int> G[NMAX];
int N , S , ans , C[NMAX];

void df(int nod)
{
	for(vector<int>::const_iterator w = G[nod].begin();w != G[nod].end();++w)
		df(*w) , C[nod]+=C[*w];

	while(C[nod] > S) {
		ans++;
		int cmax = -1 , wmax = 0;
			for(vector<int>::const_iterator w = G[nod].begin();w != G[nod].end();++w)
				if(C[*w] > cmax) cmax = C[*w] , wmax = *w;
		C[nod]-=cmax; C[wmax] = 0;
	}
}

int main()
{
	freopen("mese.in","r",stdin);
	freopen("mese.out","w",stdout);
	scanf("%d %d",&N,&S);
	for(int i = 1 , T = 0;i <= N;++i) {
		scanf("%d %d",&T,&C[i]);
		G[T].push_back(i);
	}

	df(0);

	printf("%d\n",ans + 1);
	return 0;
}