Pagini recente » Cod sursa (job #853297) | Cod sursa (job #2660641) | Cod sursa (job #1621434) | Cod sursa (job #2346127) | Cod sursa (job #984240)
Cod sursa(job #984240)
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
#include <algorithm>
#include <utility>
#include <cstring>
#include <string>
#include <stack>
#include <deque>
#include <iomanip>
#include <set>
#include <map>
#include <cassert>
#include <ctime>
using namespace std;
ifstream cin("mese.in");
ofstream cout("mese.out");
const int MAXN = 100001;
const int oo = (1<<31)-1;
typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;
const inline int min(const int &a, const int &b) { if( a > b ) return b; return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b; return a; }
const inline void Get_min(int &a, const int b) { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b) { if( a < b ) a = b; }
Graph G;
int N, S, Age[MAXN], Ans;
struct ClassComp {
inline bool operator () (const int &a, const int &b) const {
return Age[a] < Age[b];
}
};
inline void DFs(int Node) {
for (It it = G[Node].begin(), fin = G[Node].end(); it != fin ; ++ it)
DFs(*it),
Age[Node] += Age[*it];
stable_sort(G[Node].begin(), G[Node].end(), ClassComp());
for(vector<int> :: reverse_iterator it = G[Node].rbegin() ; Age[Node] > S && it != G[Node].rend() ; ) {
Age[Node] -= Age[*it];
++ Ans;
++ it;
}
}
int main() {
cin >> N >> S;
for(int i = 1 ; i <= N ; ++ i) {
int x, y;
cin >> x >> Age[i];
G[x].push_back(i);
}
DFs(0);
cout << ++ Ans << '\n';
cin.close();
cout.close();
return 0;
}