Cod sursa(job #1701604)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 13 mai 2016 17:13:24
Problema Mese Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <cstring>

using namespace std;

ifstream fin("mese.in");
ofstream fout("mese.out");

const int dim = 100005;

int n, maxSum;
int old[dim], sum[dim];

vector<int> g[dim];

int sol;
void dfs(int node) {

	sum[node] = old[node];
	vector<int> v;
	for (int adj : g[node]) {

		dfs(adj);
		v.push_back(sum[adj]);

	}

	sort(v.begin(), v.end());
	int cnt = v.size();
	for (int it : v) {

		if (sum[node] + it > maxSum)
			break;

		sum[node] += it;
		--cnt;

	}

	sol += cnt;

}

int main() {

	fin >> n >> maxSum;
	for (int i = 1; i <= n; ++i) {

		int x, y;
		fin >> x >> y;

		g[x].push_back(i);
		old[i] = y;

	}

	old[0] = maxSum + 1;
	dfs(0);
	fout << sol << '\n';

	return 0;

}

//Trust me, I'm the Doctor!