​http://www.elijahqi.win/2018/02/02/usaco2018-jan-mootube-silver/​​ ‎

数据范围弱化 暴力搞即可

#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 5500
#define inf 0x3f3f3f3f
using namespace std;
inline char gc(){
static char now[1<<16],*S,*T;
if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0,f=1;char ch=gc();
while(ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=gc();}
while(ch<='9'&&ch>='0') x=x*10+ch-'0',ch=gc();
return x*f;
}
struct node{
int y,next,z;
}data[N<<1];
int n,q,k,v,dis[N],h[N],ans,num;
inline void dfs(int x,int fa){
if (x!=v&&dis[x]>=k)++ans;
for (int i=h[x];i;i=data[i].next){
int y=data[i].y,z=data[i].z;if (y==fa) continue;
dis[y]=min(z,dis[x]);dfs(y,x);
}
}
int main(){
freopen("mootube.in","r",stdin);
freopen("mootube.out","w",stdout);
n=read();q=read();
for (int i=1;i<n;++i){
int x=read(),y=read(),z=read();
data[++num].y=y;data[num].next=h[x];data[num].z=z;h[x]=num;
data[++num].y=x;data[num].next=h[y];data[num].z=z;h[y]=num;
}
for (int i=1;i<=q;++i)
k=read(),v=read(),dis[v]=inf,ans=0,dfs(v,0),printf("%d\n",ans);
return 0;
}