# [bzoj1468]Tree（点分治）

/**************************************************************
Problem: 1468
User: stone41123
Language: C++
Result: Accepted
Time:716 ms
Memory:3324 kb
****************************************************************/

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int x=0;char ch=' ';int f=1;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')f=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*f;
}
struct edge{
int to,next,w;
}e[80001];
int n,tot,root;
ll k;
inline void addedge(int x,int y,int l){
}
int size[40001],vis[40001],mx,sz;
ll dis[40001],q[40001],l,r;
void getroot(int x,int fa){
size[x]=1;int num=0;
int u=e[i].to;
if(u==fa||vis[u])continue;
getroot(u,x);
size[x]+=size[u];
num=max(num,size[u]);
}
num=max(num,sz-size[x]);
if(num<mx){
mx=num;root=x;
}
}
void getdis(int x,int fa){
q[++r]=dis[x];
int u=e[i].to;
if(u==fa||vis[u])continue;
dis[u]=dis[x]+e[i].w;
getdis(u,x);
}
}
ll calc(int x,int v){
r=0;
dis[x]=v;
getdis(x,0);
ll sum=0;
l=1;
sort(q+1,q+r+1);
while(l<r){
if(q[l]+q[r]<=k)sum+=r-l,l++;
else r--;
}
return sum;
}
ll ans;
void dfs(int x){
ans+=calc(x,0);
vis[x]=1;
int u=e[i].to;
if(vis[u])continue;
ans-=calc(u,e[i].w);
sz=size[u];
mx=0x3f3f3f3f;
getroot(u,0);
dfs(root);
}
}
int main(){
for(int i=1;i<n;i++){