跳到正文

算法·理论

点分治

_JH_约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年7月23日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 1 分钟

版本

  1. v1当前

模板题1 P3806 【模板】点分治 1

模板题2 P4178 Tree

求树的重心

C++
int sz[N],maxp[N],rtsum,root;
bool vit[N];
/*
sz[i]表示以i为根的子树大小
maxp[i]表示删去节点i后最大的子树
vit[i]==0的集合 表示查找的范围
rtsum表示查找范围的总子树大小
root表示重心
*/
void getroot(int u,int father){
	sz[u]=1,maxp[u]=0;
	for(int i=head[u];i;i=edge[i].next){
		int v=edge[i].to;
		if(v==father||vit[v]) continue;
		getroot(v,u);
		sz[u]+=sz[v];
		maxp[u]=max(maxp[u],sz[v]);
	}
	maxp[u]=max(maxp[u],rtsum-sz[u]);
	if(maxp[u]<maxp[root]) root=u;
}
原文