<题解>世界树
世界树<题解>
首先我们拿到这个题之后,能想到的一定是虚树,如果想不到的话,还是重新学一遍去吧
所以我们应该怎么做呢
虚树的板子不需要我再讲一遍了吧
所以对于这个题来说,怎么根据虚树上的节点来找到每一个点的集合的大小
针对这种在树上求集合大小的题,不是dp就是利用siz(子树大小)来容斥求得
然而这个题就是巧妙的利用了siz这个东西
(注意接下来说的,儿子是指虚树中的儿子,子树是指原树中的子树)
vis[]数组用来标记这个节点是否为议事处
我们利用who[]和dis[]两个数组来实现在这个虚树上的容斥
who[x]表示距离x这个节点最近的被标记的节点
dis[x]表示x与who[x]之间的距离
我们想要求这两个值,就需要分为两部分去求,父亲那边的,虚树子树内的
所以我们进行两次dfs,一次向下寻找,一次向上寻找,得到这个数组
int who[N],dis[N];
void dfs_sol1(int x){
who[x]=dis[x]=inf;
if(vis[x])who[x]=x,dis[x]=0;
for(re i=head[x];i;i=nxt[i]){
int y=to[i];
dfs_sol1(y);
int tmp=DIS(x,who[y]);
if(tmp<dis[x]||tmp==dis[x]&&who[y]<who[x])who[x]=who[y],dis[x]=tmp;
}
}
void dfs_sol2(int x){
for(re i=head[x];i;i=nxt[i]){
int y=to[i];
int tmp=DIS(y,who[x]);
if(tmp<dis[y]||tmp==dis[y]&&who[x]<who[y])who[y]=who[x],dis[y]=tmp;
dfs_sol2(y);
}
}
为什么不需要分为两种,父亲的和儿子的
因为我们对于每一个节点,他和他儿子的who不一样的话
要么他儿子本身就被标记过了,要么是儿子的who在儿子的虚树子树内
所以不会出现有两组x到who[x]的路径重复的情况
所以我们只需要记录一个最近节点
那么最后我们就需要去求每个议事处的集合大小了
既然是在树上,我们很容易想到是要用到一个dfs的
那我们每遍历到一个节点就将这个节点的siz加到这个节点的who上
然后我们就进行一次判断,
如果此时节点与他的儿子节点的who相同,那我们就直接减去儿子节点的siz
如果不相同的话,我们就利用倍增的办法,找到这个分界点,使得分界点两侧一侧是分到
当前节点的who上,一侧是分到儿子节点的who上,这样我们直接利用siz进行加减就好了
如何找到分界点呢?
因为每两个点之间的距离都是1,所以这个分界点一定是这条链的中点,再特判一下,搞定中点就可以啦
为什么要用倍增??因为快
$ code $
#include<bits/stdc++.h>
using namespace std;
#define re register int
#define ll long long
#define inf 0x3f3f3f3f
const int N=300005;
int n,q,m,h[N];
int to[N*2],nxt[N*2],head[N],rp;
int dfn[N],cnt;
int dep[N],fa[N][21],siz[N];
void add_edg(int x,int y){
to[++rp]=y;
nxt[rp]=head[x];
head[x]=rp;
}
void dfs_first(int x){
dfn[x]=++cnt;
siz[x]=1;
for(re i=head[x];i;i=nxt[i]){
int y=to[i];
if(y==fa[x][0])continue;
fa[y][0]=x;
for(re i=1;i<=20;i++)fa[y][i]=fa[fa[y][i-1]][i-1];//cout<<fa[y][i]<<endl;
dep[y]=dep[x]+1;
dfs_first(y);
siz[x]+=siz[y];
}
}
int LCA(int x,int y){
if(dep[x]<dep[y])swap(x,y);
for(re i=20;i>=0;i--)
if(dep[fa[x][i]]>=dep[y])
x=fa[x][i];
if(x==y)return x;
for(re i=20;i>=0;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]-2*dep[LCA(x,y)];
}
int sta[N],tot;
bool vis[N];
bool cmp(int x,int y){
return dfn[x]<dfn[y];
}
void build_vtree(){
sort(h+1,h+m+1,cmp);
sta[tot=1]=1;head[1]=0;rp=0;
for(re i=1;i<=m;i++){
if(h[i]==1)continue;
int lca=LCA(sta[tot],h[i]);
//cout<<lca<<endl;
if(lca!=sta[tot]){
while(dfn[lca]<dfn[sta[tot-1]])
add_edg(sta[tot-1],sta[tot]),tot--;
if(lca!=sta[tot-1]){
head[lca]=0;
add_edg(lca,sta[tot]);
sta[tot]=lca;
}
else add_edg(lca,sta[tot]),tot--;
}
head[h[i]]=0;
sta[++tot]=h[i];
}
//cout<<tot<<endl;
for(re i=1;i<tot;i++)add_edg(sta[i],sta[i+1]);
}
int who[N],dis[N];
void dfs_sol1(int x){
who[x]=dis[x]=inf;
if(vis[x])who[x]=x,dis[x]=0;
for(re i=head[x];i;i=nxt[i]){
int y=to[i];
dfs_sol1(y);
int tmp=DIS(x,who[y]);
if(tmp<dis[x]||tmp==dis[x]&&who[y]<who[x])who[x]=who[y],dis[x]=tmp;
}
}
void dfs_sol2(int x){
for(re i=head[x];i;i=nxt[i]){
int y=to[i];
int tmp=DIS(y,who[x]);
if(tmp<dis[y]||tmp==dis[y]&&who[x]<who[y])who[y]=who[x],dis[y]=tmp;
dfs_sol2(y);
}
}
int get_fa(int x,int t){
for(re i=20;i>=0;i--)
if(t>=1<<i)
x=fa[x][i],t-=1<<i;
return x;
}
int ans[N];
void dfs_ans(int x){
ans[who[x]]+=siz[x];
for(re i=head[x];i;i=nxt[i]){
int y=to[i];
//cout<<x<<" "<<who[x]<<" "<<y<<" "<<who[y]<<endl;
if(who[x]==who[y])ans[who[x]]-=siz[y];
else{
int tmp=DIS(x,y)-1+dis[x]-dis[y]>>1;
if(!(DIS(who[x],who[y])&1)&&who[y]<who[x])tmp++;
int z=get_fa(y,tmp);
ans[who[x]]-=siz[z];
ans[who[y]]+=siz[z]-siz[y];
}
dfs_ans(y);
}
}
signed main(){
scanf("%d",&n);
for(re i=1,x,y;i<n;i++){
scanf("%d%d",&x,&y);
add_edg(x,y);
add_edg(y,x);
}
dep[1]=1;
dfs_first(1);
scanf("%d",&q);
int an[N];
while(q--){
scanf("%d",&m);
for(re i=1;i<=m;i++){
scanf("%d",&h[i]);
an[i]=h[i];
vis[h[i]]=1;
}
build_vtree();
dfs_sol1(1);
dfs_sol2(1);
for(re i=1;i<=m;i++)ans[an[i]]=0;
dfs_ans(1);
for(re i=1;i<=m;i++)printf("%d ",ans[an[i]]);
printf("\n");
for(re i=1;i<=m;i++)vis[h[i]]=0;
}
}
完事了。。。。。