1 /**************************************************************
2 Problem: 2500
3 User: Tunix
4 Language: C++
5 Result: Accepted
6 Time:2496 ms
7 Memory:55960 kb
8 ****************************************************************/
9
10 //Huce #1 C
11 #include<vector>
12 #include<cstdio>
13 #include<cstring>
14 #include<cstdlib>
15 #include<iostream>
16 #include<algorithm>
17 #define rep(i,n) for(int i=0;i<n;++i)
18 #define F(i,j,n) for(int i=j;i<=n;++i)
19 #define D(i,j,n) for(int i=j;i>=n;--i)
20 #define pb push_back
21 using namespace std;
22 inline int getint(){
23 int v=0,sign=1; char ch=getchar();
24 while(ch<‘0‘||ch>‘9‘){ if (ch==‘-‘) sign=-1; ch=getchar();}
25 while(ch>=‘0‘&&ch<=‘9‘){ v=v*10+ch-‘0‘; ch=getchar();}
26 return v*sign;
27 }
28 const int N=1e6+10,INF=~0u>>2;
29 typedef long long LL;
30 /******************tamplate*********************/
31 int to[N],next[N],head[N],len[N],cnt;
32 void add(int x,int y,int z){
33 to[++cnt]=y; next[cnt]=head[x]; head[x]=cnt; len[cnt]=z;
34 }
35 int n,m;
36 LL f[N],g[N][2],a[N];
37 void down(int x){
38 for(int i=head[x];i;i=next[i]){
39 down(to[i]);
40 if (g[to[i]][0]+len[i]>g[x][0]){
41 g[x][1]=g[x][0];
42 g[x][0]=g[to[i]][0]+len[i];
43 }else g[x][1]=max(g[x][1],g[to[i]][0]+len[i]);
44 }
45 }
46 void up(int x){
47 int y;
48 for(int i=head[x];i;i=next[i]){
49 f[y=to[i]]=f[x]+len[i];
50 if (g[y][0]+len[i]==g[x][0]) f[y]=max(f[y],g[x][1]+len[i]);
51 else f[y]=max(f[y],g[x][0]+len[i]);
52 up(y);
53 }
54 }
55 int Q1[N],Q2[N];
56 int main(){
57 #ifndef ONLINE_JUDGE
58 freopen("C.in","r",stdin);
59 freopen("C.out","w",stdout);
60 #endif
61 n=getint(); m=getint();
62 int x,y,z;
63 F(i,2,n){
64 x=getint(); z=getint();
65 add(x,i,z);
66 }
67 down(1); up(1);
68 F(i,1,n) a[i]=max(f[i],g[i][0]);
69 LL ret=1,ans=0;int l1=0,r1=-1,l2=0,r2=-1;
70 F(i,1,n){
71 while(l1<=r1 && a[i]<=a[Q1[r1]]) r1--;
72 Q1[++r1]=i;
73 while(l2<=r2 && a[i]>=a[Q2[r2]]) r2--;
74 Q2[++r2]=i;
75 while(a[Q2[l2]]-a[Q1[l1]]>m)
76 ret=Q1[l1]<Q2[l2] ? Q1[l1++]+1 : Q2[l2++]+1;
77 ans=max(ans,i-ret+1);
78 }
79 printf("%lld\n",ans);
80 return 0;
81 }