跳到正文

个人记录

二叉点分治 详解

天命之路约 11 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年11月11日
篇幅
约 11 分钟
获赞
1

版本

  1. v1当前

所谓二叉点分治,就是一种将点分治和边分治的优势相结合的算法。

点分治的劣势是什么?有多棵子树,当前集合中的点被分成多类,如果要统计点对之间的贡献,结构不如边分治好。

边分治的劣势是什么?要转二叉树,加入许多虚点,很麻烦,还有可能碰到解决不了的东西。

那么二叉点分治应运而生。

它的思想是找到分治中心,划分出若干棵子树,并将这些子树尽量均匀地划分到 L,RL,R 两个集合,统计 u∈L,v∈Ru \in L,v \in R 的点对 (u,v)(u,v) 的答案。,然后向 L,RL,R 递归下去。

分治中心当然还是找重心,这样每棵子树的大小都会 ≤∣S∣2\le \dfrac{|S|}{2} 。

划分集合可以类似如此:

C++
int sum = 0;
for(auto v : G[rt])
{
	sum += sze[v];
    if(sum * 2 <= S) // 划分到 L
    else // 划分到 R
}

这或许和全局平衡二叉树找带权重心有些相像。

时间复杂度是 Θ(nlog⁡n)\Theta(n \log n) 的吧。我是没找到什么卡掉的办法。

例题:P4565 [CTSC2018]暴力写挂

划式子,变为:12(dis(x,y)+dep(x)+dep(y))−dep′(lca′(x,y))\dfrac{1}{2}(dis(x,y)+dep(x)+dep(y))-dep'(lca'(x,y))

在第一棵树上跑二叉点分治,然后设 D(x)D(x) 表示 xx 到分治中心的距离,式子变为 12(D(x)+dep(x)+D(y)+dep(y))−dep′(lca′(x,y))\dfrac{1}{2}(D(x)+dep(x)+D(y)+dep(y))-dep'(lca'(x,y))

在第二棵树上对当前分治点集建虚树,在虚树上统计答案即可。

具体地,设 v(x)=D(x)+dep(x)v(x) = D(x)+dep(x),我们要统计 x∈L,y∈Rx \in L,y \in R 的点对的答案,那就记录第二棵树的子树中 LL 点中 vv 的最大值,RR 点中 vv 的最大值,在 lcalca 处拼出答案即可。

C++
#include <bits/stdc++.h>
#define FI first
#define SE second
using namespace std;
typedef tuple<int,int,int> Edge_type;
typedef pair<int,int> pii;
typedef long long ll;
const int N = 4e5 + 5;
const int inf = 0x3f3f3f3f;
const ll llinf = 0x3f3f3f3f3f3f3f3f;
int n;

vector<pii> G[N],G2[N];
vector<int> Gv[N];
ll Mx0[N],Mx1[N]; // L 点最大和 R 点最大
int Is[N];
namespace Vir{ // 建虚树,当然还有 LCA
	int dfn[N],dfstime;
	int ST[20][N * 2],lg[N * 2];
	int dep[N];
	inline int Min(int x,int y) { return dep[x] < dep[y] ? x : y;}
	void dfs(int x,int fa)
	{
		dep[x] = dep[fa] + 1;
		ST[0][dfn[x] = ++dfstime] = x;
		for(auto it : G2[x])
		{
			int y = it.first;
			if(y == fa) continue;
			dfs(y,x);
			ST[0][++dfstime] = x;
		}
	}
	void init()
	{
		dfs(1,0);lg[0] = -1;
		for(int i = 1;i <= dfstime;i++) lg[i] = lg[i >> 1] + 1;
		for(int j = 1;j <= 19;j++)
			for(int i = 1;i + (1 << j) - 1<= dfstime;i++)
				ST[j][i] = Min(ST[j - 1][i],ST[j - 1][i + (1 << j - 1)]);
	}
	int lca(int x,int y)
	{
		x = dfn[x];y = dfn[y];
		if(x > y) swap(x,y);
		int k = lg[y - x + 1];
		return Min(ST[k][x],ST[k][y-(1<<k)+1]);
	}
	int stk[N],top,Is[N];
	inline bool cmp(int x,int y) { return dfn[x] < dfn[y];}
	inline void Clr(int x) { Gv[x].clear();Mx0[x] = Mx1[x] = -llinf;Is[x] = 0;}
	inline void addvir(int x,int y) { Gv[x].push_back(y);Gv[y].push_back(x);}
	inline void build_vir(vector<int> &a)
	{
		stk[top = 1] = 1;
		Clr(1);
		sort(a.begin(),a.end(),cmp);
		for(int i = 0;i < a.size();i++)
		{
			if(a[i] == 1) continue;
			int l = lca(a[i],stk[top]);
			if(l != stk[top])
			{
				while(top && dfn[l] < dfn[stk[top - 1]])
					addvir(stk[top - 1],stk[top]),--top;
				if(dfn[l] != dfn[stk[top - 1]])
					Clr(l),addvir(stk[top],l),stk[top] = l;
				else addvir(l,stk[top]),--top;
			}
			stk[++top] = a[i];Clr(a[i]);
		}
		for(int i = 1;i < top;i++) addvir(stk[i],stk[i + 1]);
	}
}

ll D[N],dep[N],dep2[N]; // D : 分治内部,Dep:整棵树
void dfs1(int x,int fa)
{
	for(auto it : G[x])
	{
		int y = it.FI;
		if(y == fa) continue;
		dep[y] = dep[x] + it.second;
		dfs1(y,x);
	}
}
void dfs2(int x,int fa)
{
	for(auto it : G2[x])
	{
		int y = it.FI;
		if(y == fa) continue;
		dep2[y] = dep2[x] + it.second;
		dfs2(y,x);
	}
}

vector<pii> Gnow[N]; // 根据记录的边集,每次重新构造连通块
void GetD(vector<int> &Nd,int x,int fa)
{
	Nd.push_back(x); // 划分 L,R
	for(auto it : Gnow[x])
	{
		int y = it.first;
		if(y == fa) continue;
		D[y] = D[x] + it.second;
		GetD(Nd,y,x);
	}
}

// 虚树上 dp
ll Dp(int x,int fa)
{
	if(Is[x] == 1) Mx0[x] = D[x] + dep[x],Mx1[x] = -llinf;
	else if(Is[x] == 2) Mx1[x] = D[x] + dep[x],Mx0[x] = -llinf;
	ll res = -llinf;
	for(auto y : Gv[x])
	{
		if(y == fa) continue;
		res = max(res,Dp(y,x));
		res = max(res,(Mx0[x] + Mx1[y]) / 2 - dep2[x]);
		res = max(res,(Mx1[x] + Mx0[y]) / 2 - dep2[x]);
		Mx0[x] = max(Mx0[x],Mx0[y]);
		Mx1[x] = max(Mx1[x],Mx1[y]);
	}
	res = max(res,dep[x] - dep2[x]);
	return res;
}
int sze[N],mxsz[N];
void getroot(int x,int S,int fa)
{
	sze[x] = 1;
	for(auto it : Gnow[x])
	{
		int y = it.FI;
		if(y == fa) continue;
		getroot(y,S,x);
		sze[x] += sze[y];
		mxsz[x] = max(mxsz[x],sze[y]);
	}
	mxsz[x] = max(mxsz[x],S - sze[x]);
}
ll Divide(vector<int> V,vector<Edge_type> E)
{
	if(V.size() == 2)
	{
		int u = V[0],v = V[1],w = get<2>(E[0]);
		ll ans = (w + dep[u] + dep[v]) / 2 - dep2[Vir::lca(u,v)];
		return ans;
	}
	for(auto i : V)
		sze[i] = mxsz[i] = Is[i] = D[i] = 0,Gnow[i].clear();
	for(auto e : E)
	{
		int u = get<0>(e),v = get<1>(e),w = get<2>(e);
		Gnow[u].emplace_back(v,w);
		Gnow[v].emplace_back(u,w);
	}
	getroot(V[0],V.size(),0);
	int rt = V[0];
	for(auto i : V)
		if(mxsz[i] < mxsz[rt]) rt = i;
	getroot(rt,V.size(),0);
	int sum = 0;
	vector<int> L,R;
	vector<Edge_type> Le,Re;
	for(auto it : Gnow[rt])
	{
		int v = it.FI,w = it.SE;
		sum += sze[v];
		if(2 * sum <= V.size())
		{
			D[v] = w;
			GetD(L,v,rt);
			Le.emplace_back(rt,v,w);
		}
		else
		{
			D[v] = w;
			GetD(R,v,rt);
			Re.emplace_back(rt,v,w);
		}
	}
	for(auto i : L) Is[i] = 1;
	for(auto i : R) Is[i] = 2;
	Vir::build_vir(V);
	ll res = Dp(1,0);
	for(auto e : E)
	{
		int u = get<0>(e),v = get<1>(e);
		if(Is[u] == 1 && Is[v] == 1) Le.push_back(e);
		if(Is[u] == 2 && Is[v] == 2) Re.push_back(e);
	}
	L.push_back(rt);R.push_back(rt);
	res = max(res,Divide(L,Le));
	res = max(res,Divide(R,Re));
	return res;
}
vector<int> V;
vector<Edge_type> E;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n;
	for(int i = 1;i < n;i++)
	{
		int u,v,w;
		cin >> u >> v >> w;
		G[u].emplace_back(v,w);
		G[v].emplace_back(u,w);
		E.emplace_back(u,v,w);
	}
	for(int i = 1;i < n;i++)
	{
		int u,v,w;
		cin >> u >> v >> w;
		G2[u].emplace_back(v,w);
		G2[v].emplace_back(u,w);
	}
	dfs1(1,0);
	dfs2(1,0);
	Vir::init();
	for(int i = 1;i <= n;i++) V.push_back(i);
	cout << Divide(V,E) << endl;
	return 0;
}
原文
存档评论1 条抓取于