版本比较
虚树学习笔记
正文
增加 153 行删除 9 行
79 行未改动808182838485848586878990888990919495929394959697989910010110210310410510810610710810911011111211311411541 行未改动15715815916016016116116216316416516639 行未改动20620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135279 行未改动 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[top1]].push_back(st[top]); 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]); 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]); 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(logn)$(因为有排序)。两种方法复杂度均为 $O(nlogn)$(因为有排序)。
## 例题
虚树自身其实没有太多能挖掘的性质,主要是与其他知识点相结合(如树形dp等)。下取数道例题供参考。
### P2495 【模板】虚树 / [SDOI2011] 消耗战* * *原题链接[P2495](https://www.luogu.com.cn/problem/P2495)。
41 行未改动}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]) 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){39 行未改动 return 0;}```::::
### CF1320E Treeland and Viruses* * *
看到取部分点,想到建虚树。
本题要求的是,对于每个 $u$ 点,在所有 $v$ 点中,在到达时间最短的前提下来源序号最小的点。
是不是有一些最短路的味道?(至少题解里的大佬是这么觉得的。)
同时,由于到达一个点的时候可能本次传播还未结束,因此我们还要记一个剩余可走的路程。优先级排在时间和序号之后,越大越好(也可以不用,转移时直接计算出来)。
跑一遍 $dijkstra$ 即可,记得在结构体里重载小于号,按上面的来。
::::info[代码]```#include<bits/stdc++.h>#define ll long longusing 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;}```::::
~~事实证明虚树调试时最难的是不要不小心输出原树。~~