点分治板子

使用前初始化 sum=mx[rt=0]=n,getrt(1,0),solve(rt);

int sum, n, rt;
int sz[N], mx[N], vis[N];
struct _ {int to,w;};
vector<_> g[N];

void getrt(int x, int fa) {
    mx[x]=0, sz[x]=1;
    for (_ e:g[x]) if (!vis[e.to]&&e.to!=fa) {
		int y = e.to;
        getrt(y,x),sz[x]+=sz[y];
        mx[x]=max(mx[x],sz[y]);
    }
    mx[x]=max(mx[x],sum-sz[x]);
    if (mx[rt]>mx[x]) rt=x;
}
void solve(int x) {
	vis[x] = 1;
	for (_ e:g[x]) if (!vis[e.to]) {
		int y = e.to;
	}
	for (_ e:g[x]) if (!vis[e.to]) {
		mx[rt=0]=n,sum=sz[e.to];
		getrt(e.to,0), solve(rt);
	}
}
原文地址:https://www.cnblogs.com/uid001/p/10587607.html