一種簡單的建虛樹方法


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;
	}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM