一种简单的建虚树方法

low[u] 是指 u 子树内最大的 dfs 序

直接看代码:

std::vector<int> d;

inline bool cmp(int a,int b){
	return dfn[a]<dfn[b];
}

inline void build(){
	std::sort(d.begin(),d.end(),cmp);
	for(int i=d.size()-2;~i;i--)
		d.push_back(LCA(d[i],d[i+1]));
	std::sort(d.begin(),d.end(),cmp);
	d.erase(std::unique(d.begin(),d.end()),d.end());
	top=0;
	for(int i=0;i<(int)d.size();i++){
		int u=d[i];
		while(top&&low[sta[top]]<dfn[u])top--;
		if(top)fa[u]=sta[top];
		sta[++top]=u;
	}
}

 

posted @ 2021-07-31 23:33  一叶知秋‘  阅读(194)  评论(3编辑  收藏  举报