传送门
题解
1 //minamoto
2 #include<iostream>
3 #include<cstdio>
4 #include<cstring>
5 #include<vector>
6 #define ll long long
7 #define inf 0x3f3f3f3f3f3f3f3f
8 using namespace std;
9 #define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
10 char buf[1<<21],*p1=buf,*p2=buf;
11 int read(){
12 #define num ch-'0'
13 char ch;bool flag=0;int res;
14 while(!isdigit(ch=getc()))
15 (ch=='-')&&(flag=true);
16 for(res=num;isdigit(ch=getc());res=res*10+num);
17 (flag)&&(res=-res);
18 #undef num
19 return res;
20 }
21 const int N=5005;
22 struct node{ll y,n;};vector<node> f[N];
23 int head[N],Next[N],ver[N],tot;
24 inline void add(int u,int v){
25 ver[++tot]=v,Next[tot]=head[u],head[u]=tot;
26 }
27 int c[N],d[N],sz[N],n,q,m;
28 void dfs(int u){
29 ll rr,ss,tt;sz[u]=1,f[u].resize(2);
30 f[u][0].n=0,f[u][0].y=inf,f[u][1].y=c[u]-d[u],f[u][1].n=c[u];
31 for(int i=head[u];i;i=Next[i]){
32 int v=ver[i];dfs(v);
33 f[u].resize(sz[u]+sz[v]+1,(node){inf,inf});
34 for(int j=sz[u];j>=0;--j)
35 for(int k=0;k<=sz[v];++k){
36 rr=f[u][j].n+f[v][k].n,
37 ss=f[u][j].y+f[v][k].n,
38 tt=f[u][j].y+f[v][k].y;
39 if(rr<=m&&f[u][j+k].n>rr) f[u][j+k].n=rr;
40 if(ss<=m&&f[u][j+k].y>ss) f[u][j+k].y=ss;
41 if(tt<=m&&f[u][j+k].y>tt) f[u][j+k].y=tt;
42 }
43 vector<node>().swap(f[v]);
44 sz[u]+=sz[v];
45 }
46 }
47 int main(){
48 // freopen("testdata.in","r",stdin);
49 n=read(),m=read(),c[1]=read(),d[1]=read();
50 for(int i=2,fa;i<=n;++i)
51 c[i]=read(),d[i]=read(),fa=read(),add(fa,i);
52 dfs(1);
53 for(int i=n;i;--i)
54 if(f[1][i].y<=m||f[1][i].n<=m) return printf("%d
",i),0;
55 return puts("0"),0;
56 }