和上一篇博客蛮像的 问的是小于等于k的有多少个
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
const int MAX_N = 20025;
int p[MAX_N],eid,ans,dis[MAX_N],T,TT,Size[MAX_N],maxson[MAX_N],SIZE,ms,root,m;
bool vis[MAX_N];
void init()
{
memset(p,-1,sizeof(p));
eid = 0;
}
struct edge
{
int v,next,len;
}e[MAX_N];
void add(int u,int v,int len)
{
e[eid].v = v;
e[eid].len = len;
e[eid].next = p[u];
p[u] = eid++;
}
void getroot(int x,int fa)//求重心
{
Size[x] = 1;maxson[x] = 0;
for(int i = p[x];i!=-1;i=e[i].next)
{
int v = e[i].v;
if(vis[v]||v==fa) continue;
getroot(v,x);
Size[x] += Size[v];
if(Size[v]>maxson[x]) maxson[x] = Size[v];
}
if(SIZE-Size[x]>maxson[x]) maxson[x] = SIZE - Size[x];
if(ms>maxson[x]) ms = maxson[x],root = x;
}
void getdis(int x,int fa,int z)
{
dis[++T] = z;
for(int i = p[x];i!=-1;i=e[i].next)
{
int v = e[i].v;
if(v==fa||vis[v]) continue;
getdis(v,x,z+e[i].len);
}
}
struct node{int x,y;}arr[MAX_N];
bool cmp(int x,int y) {return x < y;};
void solve(int x,int y,int id)
{
T = 0;
getdis(x,0,y);
sort(dis+1,dis+1+T,cmp);
int l = 1,r = T;
while(l<=r)
{
if(dis[r]+dis[l]<=m) {ans+=(r-l)*id,l++;}
else r--;
}
}
void fenzhi(int x,int ssize)//ssize是当前这棵子树的大小
{
vis[x] = true;
solve(x,0,1);
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;
if(vis[v]) continue;
solve(v,e[i].len,-1);//减去不合法
ms = inf,root = 0;//记得初始化
SIZE = Size[v]<Size[x]?Size[v]:(ssize-Size[x]);
getroot(v,0);//求以y为根的字数
fenzhi(root,(Size[v]<Size[x]?Size[v]:(ssize-Size[x])));
}
}
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n,x,y,z;
while(scanf("%d%d",&n,&m)==2)
{
ans = 0;
memset(vis,false,sizeof(bool)*(n+1));
if((!n)&&(!m)) break;
init();
for(int i = 1;i<n;++i)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
root = 0;ms = inf;SIZE = n;
getroot(1,0);
fenzhi(root,n);
printf("%d\n",ans);
}
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}