1 #include<cstring>
2 #include<cmath>
3 #include<iostream>
4 #include<algorithm>
5 #include<cstdio>
6 #include<map>
7 #include<list>
8
9 #define N 2000007
10 #define inf 1000000007
11 #define fzy pair<int,int>
12 using namespace std;
13 inline int read()
14 {
15 int x=0,f=1;char ch=getchar();
16 while(ch<‘0‘||ch>‘9‘){if (ch==‘-‘) f=-1;ch=getchar();}
17 while(ch>=‘0‘&&ch<=‘9‘){x=(x<<3)+(x<<1)+ch-‘0‘;ch=getchar();}
18 return x*f;
19 }
20
21 int n,L,num,siz;
22 int hd,tl,q[N],b[N],f[N];
23 struct Node
24 {
25 int x,y,l;
26 }a[N];
27 list<fzy>tr[N],bj[N];
28 map<int,int>p;
29
30
31 void ins(int p,int l,int r,int x,int y,fzy zhi)
32 {
33 if (x<=l&&r<=y)
34 {
35 while(!tr[p].empty()&&tr[p].back().second>=zhi.second)
36 tr[p].pop_back();
37 tr[p].push_back(zhi);
38 return;
39 }
40
41 while(!bj[p].empty()&&bj[p].back().second>=zhi.second)
42 bj[p].pop_back();
43 bj[p].push_back(zhi);
44
45 int mid=(l+r)>>1;
46 if (y<=mid) ins(p<<1,l,mid,x,y,zhi);
47 else if (x>mid) ins(p<<1|1,mid+1,r,x,y,zhi);
48 else ins(p<<1,l,mid,x,mid,zhi),ins(p<<1|1,mid+1,r,mid+1,y,zhi);
49 }
50 int query(int p,int l,int r,int x,int y,int wei)
51 {
52 int res;
53
54 while(wei-tr[p].front().first>L&&!tr[p].empty())
55 tr[p].pop_front();
56 if (tr[p].empty()) res=inf;
57 else res=tr[p].front().second;
58
59
60 while(wei-bj[p].front().first>L&&!bj[p].empty()) bj[p].pop_front();
61 if (x<=l&&r<=y)
62 {
63 if (!bj[p].empty()) res=min(res,bj[p].front().second);
64 return res;
65 }
66
67 int mid=(l+r)>>1;
68 if (y<=mid) res=min(res,query(p<<1,l,mid,x,y,wei));
69 else if (x>mid) res=min(res,query(p<<1|1,mid+1,r,x,y,wei));
70 else res=min(res,min(query(p<<1,l,mid,x,mid,wei),query(p<<1|1,mid+1,r,mid+1,y,wei)));
71 return res;
72 }
73 int main()
74 {
75 n=read(),L=read();
76 for (int i=1;i<n;i++)
77 a[i].x=read(),a[i].y=read(),a[i].l=read(),b[++num]=a[i].x,b[++num]=a[i].y;
78 sort(b+1,b+num+1);
79 for (int i=1;i<=num;i++)
80 if (b[i]!=b[i-1]) p[b[i]]=++siz;
81 for (int i=1;i<n;i++)
82 a[i].x=p[a[i].x],a[i].y=p[a[i].y];
83
84 ins(1,1,siz,1,siz,make_pair(0,0));
85 for (int i=1;i<n;i++)
86 {
87 f[i]=query(1,1,siz,a[i].x,a[i].y,a[i].l)+1;
88 ins(1,1,siz,a[i].x,a[i].y,make_pair(a[i].l,f[i]));
89 }
90 for (int i=1;i<n;i++)
91 printf("%d\n",f[i]>=n?-1:f[i]);
92 }