【模板】虚树

也不是很懂原理,反正就是这么写的。

void insert(int x)
{
	if(!top){s[++top]=x;return;}
	int LCA=lca(x,s[top]);
	while(top>1&&dep[s[top-1]]>=dep[LCA])add(s[top],s[top-1],dis(s[top],s[top-1])),top--;
	if(LCA!=s[top])add(s[top],LCA,dis(s[top],LCA)),s[top]=LCA;
	s[++top]=x;
}
posted @ 2019-01-19 22:53  Creed-qwq  阅读(166)  评论(0编辑  收藏  举报