虚树学习笔记
No_98
·
·
算法·理论
在树论中,我们有时会遇见这种类型的题目:给定一棵树,再给定若干个询问,每个询问给定一个点集,将这些点单独拉出来做询问。这时,往往要用虚树。
算法分析
虚树的思想在于,既然每次只取一些点,那么我们就只取这些点和它们的 lca (称作关键点)构成一棵虚树,在这棵树上做普通的树上操作或询问即可。
而事实上,关于虚树的点集部分,我们只需将所有初始点按 dfs 序递增来排序,再将相邻的点两两的 lca 加入点集即可。因此虚树点集的数量不超过原点集数量的两倍,这也是虚树时间复杂度正确的原因。
下面就是虚树具体形态构建的问题了。有两种常用方法:二次排序和单调栈。两种方法各有千秋,下面逐一介绍。
建树法一:二次排序
首先,像上文一样求出点集,去重,按 dfs 序排好。
然后,对于相邻的两点,求出 lca,在 lca 和后点间连一条边。好了。
好了?!!
好了。
::::info[证明]
记相邻两点为 x,y。
假如 x 是 y 的祖先,那么它们之间是没有其他关键点的,应该连一条边。
| 假如不是,y 和 lca 间应当没有其他关键点了(如果有,那么这个点是不是应该在 x,y 之间呢),应该连一条边。 |
::::info[代码]
len=k;
sort(s+1,s+k+1,cmp);
for(int i=2;i<=len;i++) s[++k]=lca(s[i-1],s[i]);
sort(s+1,s+k+1,cmp);
k=unique(s+1,s+k+1)-s-1;
for(int i=2;i<=k;i++){
int x=lca(s[i-1],s[i]);
Gn[x].push_back(edge{s[i],getnum(x,s[i])});
}
::::
优点:好写。缺点:常数大(排序两次,两两求 lca 两次)。
建树法二:单调栈
单调栈里存一条关键点的链,依次按 dfs 序插入原点。
求出栈顶和当前点的 lca,假如栈顶就是 lca,说明此点在该链上,入栈即可。
否则,退栈直到栈中次大点的深度不大于 lca。在退的过程中将退的点连起来。
假如次大点就是 lca,将栈顶和栈中次大点连边,退栈顶,将关键点入栈。
否则,将栈顶和 lca 连边,将 lca、关键点入栈。
所有点遍历过后,清空栈并连边。
以此树举例(红色是初始点,黑色是加入顺序)。
首先,将1、2、3、4入栈。
求出4、5的 lca 。发现5不在链上,退栈直到栈中次大点深度不大于 lca 的时候(此时4退栈)。
求出5、6的 $lca$。发现6不在链上,退栈直到栈中次大点深度不大于 $lca$ 的时候(此时5退栈)。
$lca$ 此时在栈中,退4、5的 $lca$,加入6。
最后,清空栈。
::::info[代码]
```
top=1,st[top]=1;
sort(h+1,h+m+1,cmp);
for(int i=1;i<=m;i++){
if(h[i]==1) continue;
int l=lca(h[i],st[top]);
if(l!=st[top]){
while(dep[l]<dep[st[top-1]]){
Gn[st[top]].push_back(st[top-1]),Gn[st[top-1]].push_back(st[top]);
top--;
}
if(l!=st[top-1]){
Gn[st[top]].push_back(l),Gn[l].push_back(st[top]);
st[top]=l;
}
else{
Gn[st[top]].push_back(l),Gn[l].push_back(st[top]);
top--;
}
}
st[++top]=h[i];
}
for(int i=1;i<top;i++) Gn[st[i]].push_back(st[i+1]),Gn[st[i+1]].push_back(st[i]);
```
::::
事实上,为了代码的方便,一般会虚空指定一个点当栈一开始的栈顶(一般为1,但要注意原关键点是否有1,且有时会有问题,也可以是0)。
比较难理解,但常数小一点。
两种方法复杂度均为 $O(nlogn)$(因为有排序)。
## 例题
虚树自身其实没有太多能挖掘的性质,主要是与其他知识点相结合(如树形dp等)。下取数道例题供参考。
### P2495 【模板】虚树 / [SDOI2011] 消耗战
* * *
原题链接[P2495](https://www.luogu.com.cn/problem/P2495)。
先考虑原树DP。
记 $f_{i}$ 为以 $i$ 为根合法的最小代价。
对于节点 $u$,对于每个儿子 $v$,记切割代价为 $w$,假如 $v$ 有能源,那么必须切断连接,代价取 $w$;否则,代价取 $\min(f_{v},w)$。
然后只需把虚树建出来,在虚树上跑DP即可。
::::info[代码]
```
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=5e5+5;
int n,dfn[N],tot,f[N][32],dep[N],mark[N],
q,s[N],len;
ll dp[N],w[N][32];
struct edge{
int to;
ll w;
};
vector<edge> G[N],Gn[N];
void dfs(int x,int fa){
dep[x]=dep[fa]+1;
f[x][0]=fa;
dfn[x]=++tot;
for(edge tmp:G[x]){
int u=tmp.to;ll v=tmp.w;
if(u==fa) continue;
w[u][0]=v;
dfs(u,x);
}
}
bool cmp(int x,int y){return dfn[x]<dfn[y];}
int lca(int x,int y){
if(dep[x]<dep[y]) swap(x,y);
for(int i=30;i>=0;i--) if(dep[f[x][i]]>=dep[y]) x=f[x][i];
if(x==y) return x;
for(int i=30;i>=0;i--) if(f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];
return f[x][0];
}
int getnum(int x,int y){
ll res=1e9;
if(dep[x]<dep[y]) swap(x,y);
for(int i=30;i>=0;i--)
if(dep[f[x][i]]>=dep[y])
res=min(res,w[x][i]),x=f[x][i];
return res;
}
void solve(int x){
dp[x]=0;
for(edge tmp:Gn[x]){
int u=tmp.to;
ll v=tmp.w;
solve(u);
if(mark[u]) dp[x]+=v;
else dp[x]+=min(v,dp[u]);
}
}
int main(){
scanf("%d",&n);
for(int i=1,u,v,w;i<n;i++){
scanf("%d%d%d",&u,&v,&w);
G[u].push_back(edge{v,w}),G[v].push_back(edge{u,w});
}
w[1][0]=1e9;
dfs(1,0);
for(int j=1;j<=30;j++)
for(int i=1;i<=n;i++)
f[i][j]=f[f[i][j-1]][j-1],
w[i][j]=min(w[i][j-1],w[f[i][j-1]][j-1]);
scanf("%d",&q);
while(q--){
int k;
scanf("%d",&k);
for(int i=1;i<=k;i++) scanf("%d",&s[i]),mark[s[i]]=1;
s[++k]=1;len=k;
sort(s+1,s+k+1,cmp);
for(int i=2;i<=len;i++) s[++k]=lca(s[i-1],s[i]);
sort(s+1,s+k+1,cmp);
k=unique(s+1,s+k+1)-s-1;
for(int i=2;i<=k;i++){
int x=lca(s[i-1],s[i]);
Gn[x].push_back(edge{s[i],getnum(x,s[i])});
}
solve(1);
printf("%lld\n",dp[1]);
for(int i=1;i<=k;i++) Gn[s[i]].clear(),mark[s[i]]=0;
}
return 0;
}
```
::::
### CF1320E Treeland and Viruses
* * *
看到取部分点,想到建虚树。
本题要求的是,对于每个 $u$ 点,在所有 $v$ 点中,在到达时间最短的前提下来源序号最小的点。
是不是有一些最短路的味道?(至少题解里的大佬是这么觉得的。)
同时,由于到达一个点的时候可能本次传播还未结束,因此我们还要记一个剩余可走的路程。优先级排在时间和序号之后,越大越好(也可以不用,转移时直接计算出来)。
跑一遍 $dijkstra$ 即可,记得在结构体里重载小于号,按上面的来。
::::info[代码]
```
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2e5+5;
int n,q,k,m,pv[N],ps[N],pu[N],vis[N],
trsz[N],trtop[N],trfa[N],trson[N],trdfn[N],trid[N],trdep[N],
vst[N],vtop,vp[N],vlen;
vector<int> G[N],Gn[N];
struct node{
int t,col,num,ct;
bool operator<(const node &f) const{
return t==f.t?(col==f.col?num<f.num:col>f.col):t>f.t;
}
};
void trdfs1(int x,int fa){
trsz[x]=1,trfa[x]=fa,trdep[x]=trdep[fa]+1;
for(int u:G[x]){
if(u==fa) continue;
trdfs1(u,x);
if(trsz[u]>trsz[trson[x]]) trson[x]=u;
}
}
void trdfs2(int x,int fa,int top){
trtop[x]=top,trdfn[x]=++trdfn[0],trid[trdfn[0]]=x;
if(trson[x]) trdfs2(trson[x],x,trtop[x]);
for(int u:G[x]){
if(u==fa||u==trson[x]) continue;
trdfs2(u,x,u);
}
}
int getlca(int u,int v){
while(trtop[u]!=trtop[v]){
if(trdep[trtop[u]]<trdep[trtop[v]]) swap(u,v);
u=trfa[trtop[u]];
}
return trdep[u]<trdep[v]?u:v;
}
bool vcmp(int u,int v){
return trdfn[u]<trdfn[v];
}
void build(){
sort(vp+1,vp+vlen+1,vcmp);
vlen=unique(vp+1,vp+vlen+1)-vp-1;
vst[1]=1,vtop=1;
for(int i=1;i<=vlen;i++){
if(vp[i]==1) continue;
int l=getlca(vst[vtop],vp[i]);
if(l!=vst[vtop]){
while(trdep[vst[vtop-1]]>trdep[l]){
Gn[vst[vtop-1]].push_back(vst[vtop]),Gn[vst[vtop]].push_back(vst[vtop-1]);
vtop--;
}
if(l!=vst[vtop-1]){
Gn[vst[vtop]].push_back(l),Gn[l].push_back(vst[vtop]);
vst[vtop]=l;
}
else{
Gn[vst[vtop]].push_back(l),Gn[l].push_back(vst[vtop]);
vtop--;
}
}
vst[++vtop]=vp[i];
}
for(int i=vtop;i>=2;i--) Gn[vst[i]].push_back(vst[i-1]),Gn[vst[i-1]].push_back(vst[i]);
}
int getw(int u,int v){
return trdep[u]+trdep[v]-trdep[getlca(u,v)]*2;
}
void dij(){
priority_queue<node> q;
for(int i=1;i<=k;i++) q.push(node{0,i,0,pv[i]});
while(!q.empty()){
node tmp=q.top();q.pop();
if(vis[tmp.ct]) continue;
vis[tmp.ct]=tmp.col;
for(int u:Gn[tmp.ct]){
if(vis[u]) continue;
int w=getw(u,tmp.ct);
if(tmp.num>=w){
q.push(node{tmp.t,tmp.col,tmp.num-w,u});
continue;
}
int pc=(w-tmp.num+ps[tmp.col]-1)/ps[tmp.col];
q.push(node{tmp.t+pc,tmp.col,pc*ps[tmp.col]+tmp.num-w,u});
}
}
}
int main(){
scanf("%d",&n);
for(int i=2,u,v;i<=n;i++){
scanf("%d%d",&u,&v);
G[u].push_back(v),G[v].push_back(u);
}
trdfs1(1,0);
trdfs2(1,0,1);
scanf("%d",&q);
while(q--){
vlen=0;
scanf("%d%d",&k,&m);
for(int i=1;i<=k;i++){
scanf("%d%d",&pv[i],&ps[i]);
vp[++vlen]=pv[i];
}
for(int i=1;i<=m;i++){
scanf("%d",&pu[i]);
vp[++vlen]=pu[i];
}
build();
dij();
for(int i=1;i<=m;i++) printf("%d ",vis[pu[i]]);
printf("\n");
for(int i=1;i<=vlen;i++){
Gn[vp[i]].clear(),vis[vp[i]]=0;
if(i>=2){
int l=getlca(vp[i],vp[i-1]);
Gn[l].clear(),vis[l]=0;
}
}
Gn[1].clear(),vis[1]=0;
}
return 0;
}
```
::::
~~事实证明虚树调试时最难的是不要不小心输出原树。~~