RuntimeErr @ 2022-10-24 13:23:00
RT,不知道哪里写挂了,参解改还是有问题
#define pb push_back
const int N=3e5+10;
#define int long long
int n,q;
vector<int>e[N];
int d[N],dep[N],siz[N],son[N];
int buff[N<<2],*now=buff,*f[N],tag[N];
void dfs1(int u,int fa){
d[u]=d[fa]+1;siz[u]=1;
for(int v:e[u]){
if(v==fa)continue;
dfs1(v,u);siz[u]+=siz[v];
if(dep[v]>dep[son[u]])son[u]=v;
}dep[u]=dep[son[u]]+1;
}
void dfs2(int u,int fa){
if(son[u])f[son[u]]=f[u]+1,dfs2(son[u],u),tag[u]=tag[son[u]]+siz[son[u]]-1;
for(int v:e[u]){
if(v==fa||v==son[u])continue;
f[v]=now,now+=dep[v];dfs2(v,u);tag[u]+=tag[v]+siz[v]-1;
for(int i=1;i<=dep[v];++i)f[u][i]+=f[v][i-1];
}f[u][0]=-tag[u];
}
signed main(){
read(n),read(q);
for(int i=1,u,v;i<n;++i){
read(u),read(v);
e[u].pb(v),e[v].pb(u);
}dfs1(1,0);f[1]=now,now+=dep[1];dfs2(1,0);
for(int i=1,p,k;i<=q;++i){
read(p),read(k);
printf("%lld\n",1ll*min(d[p]-1,k)*(siz[p]-1)+f[p][min(dep[p]-1,k)]+tag[p]);
}
return 0;
}