Cod sursa(job #1836815)

Utilizator lflorin29Florin Laiu lflorin29 Data 28 decembrie 2016 18:09:17
Problema Mese Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <bits/stdc++.h>

using namespace std;

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

const int Maxn = 1e5;

int n, s, root, v[Maxn + 1];
vector<int>sons[Maxn + 1];
int sum[Maxn + 1];
vector<int>sons_sums;

void Read() {
  in >> n >> s;

  for (int i = 1; i <= n; ++i) {
    int father;
    in >> father >> v[i];

    if (!father) {
      root = i;
    }
    else
      sons[father].push_back(i);
  }
}

void Dfs(int node, int &need) {
  sum[node] = v[node];

  for (auto fiu : sons[node]) {
    Dfs(fiu, need);
  }

  sons_sums.clear();

  for (auto fiu : sons[node])
    sons_sums.push_back(sum[fiu]);

  sort(sons_sums.begin(), sons_sums.end());

  for (int i = 0; i < (int)sons_sums.size() && sons_sums[i] + sum[node] <= s; ++i)
    sum[node] += sons_sums[i], need--;
}

int main() {
  Read();
  int need = n;
  Dfs(root, need);
  out << need;
  return 0;
}