题目
题解
构建虚树以后两遍
d
p
dp
dp处理出虚树上每个点最近的议事处
然后枚举虚树上每一条边,考虑其对两端点的答案贡献
可以用倍增二分出分界点
如果
a
a
a,
b
b
b的分界点为
m
i
d
mid
mid,
a
a
a,
b
b
b路径上
a
a
a的第一个儿子为
x
x
x
则对
a
a
a的贡献是
s
i
z
e
[
x
]
−
s
i
z
e
[
m
i
d
]
size[x]-size[mid]
size[x]−size[mid]
对
b
b
b的贡献是
s
i
z
e
[
m
i
d
]
−
s
i
z
e
[
b
]
size[mid]-size[b]
size[mid]−size[b]
还要算上没被考虑的点
Code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=300002;
struct node{
int to,ne;
}e[N*2];
int h[N],dep[N],id[N],tot,cnt,fa[N][19],f[N],a[N],b[N],bel[N],sz[N],c[N],rem[N],n,m,i,x,y,top,st[N],q;
inline char gc(){
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int rd(){
int x=0,fl=1;char ch=gc();
for(;ch<48||ch>57;ch=gc())if(ch=='-')fl=-1;
for(;48<=ch&&ch<=57;ch=gc())x=(x<<3)+(x<<1)+(ch^48);
return x*fl;
}
inline void wri(ll a){if(a<0)a=-a,putchar('-');if(a>=10)wri(a/10);putchar(a%10|48);}
inline void wln(ll a){wri(a),puts("");}
void add(int x,int y){e[++tot]=(node){y,h[x]},h[x]=tot;}
bool cmp(int x,int y){return id[x]<id[y];}
int LCA(int x,int y){
if (dep[x]<dep[y]) swap(x,y);
for (int i=18;~i;i--)
if (dep[fa[x][i]]>=dep[y]) x=fa[x][i];
if (x==y) return x;
for (int i=18;~i;i--)
if (fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
return fa[x][0];
}
int dis(int x,int y){return dep[x]+dep[y]-(dep[LCA(x,y)]<<1);}
void dfs(int u,int f){
fa[u][0]=f,id[u]=++cnt,sz[u]=1;
for (int i=1;i<19;i++) fa[u][i]=fa[fa[u][i-1]][i-1];
for (int i=h[u],v;i;i=e[i].ne)
if ((v=e[i].to)!=f) dep[v]=dep[u]+1,dfs(v,u),sz[u]+=sz[v];
}
void dfs1(int u){
c[++cnt]=u,rem[u]=sz[u];
for (int i=h[u],v;i;i=e[i].ne){
dfs1(v=e[i].to);
if (!bel[v]) continue;
int t1=dis(u,bel[v]),t2=dis(u,bel[u]);
if (!bel[u] || t1<t2 || t1==t2 && bel[v]<bel[u]) bel[u]=bel[v];
}
}
void dfs2(int u){
for (int i=h[u],v;i;i=e[i].ne){
v=e[i].to;
int t1=dis(v,bel[u]),t2=dis(v,bel[v]);
if (!bel[v] || t1<t2 || t1==t2 && bel[u]<bel[v]) bel[v]=bel[u];
dfs2(v);
}
}
void solve(int a,int b){
int x=b,mid=b;
for (int i=18;~i;i--)
if (dep[fa[x][i]]>dep[a]) x=fa[x][i];
rem[a]-=sz[x];
if (bel[a]==bel[b]){
f[bel[a]]+=sz[x]-sz[b];
return;
}
for (int i=18;~i;i--){
int ne=fa[mid][i];
if (dep[ne]<=dep[a]) continue;
int t1=dis(bel[b],ne),t2=dis(bel[a],ne);
if (t1<t2 || t1==t2 && bel[b]<bel[a]) mid=ne;
}
f[bel[a]]+=sz[x]-sz[mid];
f[bel[b]]+=sz[mid]-sz[b];
}
void query(){
m=rd();
for (int i=1;i<=m;i++) a[i]=b[i]=rd(),bel[a[i]]=a[i];
sort(a+1,a+m+1,cmp);
if (bel[1]!=1) st[++top]=1;
tot=cnt=0;
for (int i=1;i<=m;i++){
int t=a[i],lca=0;
while (top){
lca=LCA(st[top],t);
if (top>1 && dep[lca]<dep[st[top-1]]) add(st[top-1],st[top]),top--;
else{
if (dep[lca]<dep[st[top]]) add(lca,st[top]),top--;
break;
}
}
if (st[top]!=lca) st[++top]=lca;
st[++top]=t;
}
top--;
while (top) add(st[top],st[top+1]),top--;
dfs1(1),dfs2(1);
for (int i=1;i<=cnt;i++)
for (int j=h[c[i]];j;j=e[j].ne) solve(c[i],e[j].to);
for (int i=1;i<=cnt;i++) f[bel[c[i]]]+=rem[c[i]];
for (int i=1;i<=m;i++) wri(f[b[i]]),putchar(i==m?'\n':' ');
for (int i=1;i<=cnt;i++) h[c[i]]=bel[c[i]]=f[c[i]]=rem[c[i]]=0;
}
int main(){
n=rd();
for (i=1;i<n;i++) x=rd(),y=rd(),add(x,y),add(y,x);
dep[0]=-1,dfs(1,0),memset(h,0,(n+1)<<2);
for (q=rd();q--;) query();
}