# 516D Drazil and Morning Exercise

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define int long long
priority_queue<int>q[100100];
int n,m,Q,L,d[100100],rt,ans;
vector<pair<int,int> >v[100100];
inline void dfs(int x,int fa){
for(int i=0;i<v[x].size();i++)if(v[x][i].fi!=fa){
int y=v[x][i].fi,z=v[x][i].se;d[y]=d[x]+z;dfs(y,x);
}
}
inline void dfs2(int x,int fa,int sum){
d[x]=max(d[x],sum);
for(int i=0;i<v[x].size();i++)if(v[x][i].fi!=fa){
int y=v[x][i].fi,z=v[x][i].se;dfs2(y,x,sum+z);
}
}
inline void mer(priority_queue<int> &x,priority_queue<int> &y){
if((int)y.size()>(int)x.size())swap(x,y);while(!y.empty())x.push(y.top()),y.pop();
}
inline void go(int x,int fa){
for(int i=0;i<v[x].size();i++)if(v[x][i].fi!=fa){
int y=v[x][i].fi,z=v[x][i].se;go(y,x);mer(q[x],q[y]);
while(!q[x].empty())if(q[x].top()-d[x]>L)q[x].pop();else break;
}
ans=max(ans,(int)q[x].size());return;
}
signed main(){
int i,j,k;rt=1;
scanf("%lld",&n);
for(i=1;i<n;i++){
int x,y,z;
scanf("%lld%lld%lld",&x,&y,&z);
v[x].pb(mp(y,z));
v[y].pb(mp(x,z));
}
dfs(rt,0);for(i=1;i<=n;i++)if(d[i]>d[rt])rt=i;
memset(d,0,sizeof(d));dfs(rt,0);int ano=rt;
for(i=1;i<=n;i++)if(d[i]>d[ano])ano=i;dfs2(ano,0,0);
for(i=1;i<=n;i++)if(d[i]<d[rt])rt=i;
scanf("%lld",&Q);
while(Q--){
for(i=1;i<=n;i++){while(!q[i].empty())q[i].pop();q[i].push(d[i]);}
scanf("%lld",&L);ans=0;go(rt,0);printf("%lld\n",ans);
}
return 0;
}

posted @ 2019-11-08 15:15  水题收割者  阅读(144)  评论(0编辑  收藏  举报