Virtual Tree
一棵虚伪的树(雾~
虚树的主要作用:将很多点构成的树变成不太多点构成的树
其实就是将树中的关键信息(关键点)提取出来重新建树
关键点的lca也是很关键的!
所以它也要加入虚树中
重要步骤:
1.确定哪些点是关键点
2.将关键点之间的信息”缩”起来并建树
那么将在树上把dfs序跑出来
然后将当前关键点排序,然后加入相邻两个关键点的lca就行了
可以证明,这样的lca必然是等价于所有关键点两两之间的lca的(留待读者证明)
然后将这些关键点和他们的lca按照原来的关系建树即可
怎么建?我也不会
感觉很不可做的样子
在这里提出一种单调栈做法:用单调栈来维护树上的链,也就是指栈里相邻节点必然是父子关系(下面父上面子)
首先将1号节点加入虚树/栈中
然后接下来按照Dfs序从小到达添加关键节点
假如当前节点与栈顶节点的lca就是栈顶节点的话,则说明栈顶节点应该是当前节点的父亲,所以直接把当前节点入栈就行了
假如当前节点与栈顶节点的lca不是栈顶节点的话,则说明栈顶节点和当前节点不在一条链上,所以将栈里面的点弹出并且在虚树上连接好(因为他们所在的那一条链父子关系已经确定),直到栈顶的dfs序小于lca的dfs序,再将当前节点入栈即可
BZOJ 2286 消耗战 (模板)
#include <bits/stdc++.h>
using namespace std;
const int N=300000;
const int INF=2100000000;
typedef long long ll;
typedef pair<int,ll> p;
int dfn[N],fa[N][23],sta[N],top,n,m,deep[N],DFN,maxdeep,maxn,h[N],key[N];
ll f[N],sum[N][23];
struct edge {
int head[N];
int t[N<<1];
int n[N<<1];
ll c[N<<1];
int tot;
void addedge(int u,int v,ll w) {
++tot;
t[tot]=v;
c[tot]=w;
n[tot]=head[u];
head[u]=tot;
}
}e,ne;
int read();
void dfs(int);
void prepare();
bool cmp(int,int);
p lca(int,int);
void dp(int);
int main() {
n=read();
for(int i=1;i<n;i++) {
int x=read(),y=read();
ll z=read();
e.addedge(x,y,z);
e.addedge(y,x,z);
}
deep[1]=1;
dfs(1);
prepare();
m=read();
while(m--) {
int k=read();
for(int i=1;i<=k;i++) h[i]=read(),key[h[i]]=1;
sort(h+1,h+k+1,cmp);
ne.tot=0;ne.head[1]=0;sta[top=1]=1;
for(int i=1;i<=k;i++) {
if(h[i]==1) continue;
p l=lca(h[i],sta[top]);
if(l.first!=sta[top]) {
while(dfn[l.first]<dfn[sta[top-1]]) {
p nl=lca(sta[top-1],sta[top]);
ne.addedge(sta[top-1],sta[top],nl.second);
--top;
}
if(dfn[l.first]>dfn[sta[top-1]]) {
ne.head[l.first]=0;
p nl=lca(l.first,sta[top]);
ne.addedge(l.first,sta[top],nl.second);
sta[top]=l.first;
}
else {
p nl=lca(l.first,sta[top]);
ne.addedge(l.first,sta[top--],nl.second);
}
}
ne.head[h[i]]=0;
sta[++top]=h[i];
}
for(int i=1;i<top;i++) {
p l=lca(sta[i],sta[i+1]);
ne.addedge(sta[i],sta[i+1],l.second);
}
dp(1);
for(int i=1;i<=k;i++) key[h[i]]=0;
}
return 0;
}
int read() {
int ans=0,flag=1;
char ch=getchar();
while((ch>'9' || ch<'0') && ch!='-') ch=getchar();
if(ch=='-') flag=-1,ch=getchar();
while(ch>='0' && ch<='9') ans=ans*10+ch-'0',ch=getchar();
return ans*flag;
}
void dfs(int x) {
for(int i=e.head[x];i;i=e.n[i]) {
if(e.t[i]==fa[x][0]) continue;
dfn[e.t[i]]=++DFN;
fa[e.t[i]][0]=x;
sum[e.t[i]][0]=e.c[i];
deep[e.t[i]]=deep[x]+1;
maxdeep=max(maxdeep,deep[e.t[i]]);
dfs(e.t[i]);
}
}
void prepare() {
for(maxn=0;(1<<maxn)<=maxdeep;maxn++);
for(int l=1;l<=maxn;l++)
for(int i=1;i<=n;i++) {
fa[i][l]=fa[fa[i][l-1]][l-1];
sum[i][l]=min(sum[i][l-1],sum[fa[i][l-1]][l-1]);
}
}
bool cmp(int a, int b){return dfn[a]<dfn[b];}
p lca(int x,int y) {
ll ans=INF;
if(deep[x]<deep[y]) swap(x,y);
for(int i=maxn;i>=0;i--)
if(deep[fa[x][i]]>=deep[y]) {
ans=min(ans,sum[x][i]);
x=fa[x][i];
}
if(x==y)
return (p){x,ans};
for(int i=maxn;i>=0;i--)
if(fa[x][i]!=fa[y][i]) {
ans=min(ans,sum[x][i]);
ans=min(ans,sum[y][i]);
x=fa[x][i];
y=fa[y][i];
}
ans=min(ans,sum[x][0]);
ans=min(ans,sum[y][0]);
return (p){fa[x][0],ans};
}
void dp(int x) {
f[x]=0;
for(int i=ne.head[x];i;i=ne.n[i]) {
dp(ne.t[i]);
if(key[ne.t[i]]) f[x]+=ne.c[i];
else f[x]+=min(ne.c[i],f[ne.t[i]]);
}
if(x==1) printf("%lld\n",f[1]);
}
1万+

被折叠的 条评论
为什么被折叠?



