跳到正文

个人记录

浅谈点分治

Lunacturne_约 8 分钟
原文
文章信息与版本

文章信息

发布
2022年10月3日
收录
2026年1月16日
更新
2026年11月11日
篇幅
约 8 分钟

版本

  1. v2当前
  2. v1

点分治,个人认为是用来解决 无根树 上的一些点对路径信息问题的优秀算法,许多树论的题目都会用到。但是它不是一个具体的算法,就像“分治”一样,点分治也只是树上的分治的一种思路、一个框架,具体题目还是要具体分析。

首先,何为点分治?还是先看一道模板题:Luogu P3806 【模板】点分治1。

题目大意:给定一棵有权无根树,求是否存在距离为 dd 的两点。

感觉是一个很基础的题,对吧,但同样也很难想到一个较为优秀的算法(许多其他讲点分治的博客都是一上来就是重心什么的,这篇博客尝试还原点分治的思考过程)。

那么怎么办?我们肯定需要通过 DFS 来解决这个问题,但是从哪个点开始呢?很多人一上来就会说:那肯定是 1 啊,这还用说?但是别忘了,这可是一棵无根树。怎么办?认为的给他规定一个根呗,设为 xx,这就是点分治的开端。

那么规定一个根以后干什么呢?你就会想 DFS 里面干什么,然后发现还是不会做。在草稿纸上画一画图,或者一上来就可以凭直觉会发现一个很显然的事情,就是路径可以分 2 类:

  1. 经过 xx 的
  2. 不经过 xx 的

其中不经过 xx 的换种说法就是在 xx 的子树里的,你此时就会觉得这是否就是把在点 xx 上的问题分解成了在 xx 的一些子树里的问题?

答案是肯定的,因为这棵树是无根树,也就是说子树是相对独立的问题!你现在需要做的就是解决“经过 xx 的”问题,这样的问题一下子就简单多了,你会发现经过 xx 的路径也分 2 类:

  1. xx 到子树里的某一个点
  2. 子树里的一个点到另一个点,且经过 xx

对于第 2 类的情况,它其实不就是 xx 到子树里的某一个点再加上 xx 到子树里的另一个点吗!这样你就可以先计算 xx 到子树里的距离,然后判断枚举每一个距离,判断 d−curd-cur 是否存在不就行了吗?(curcur 表示当前枚举到的距离)

哈哈嗨,一阵高高兴以后,你发现了一个严峻的问题:时间复杂度?然后感觉凉了半截,发现链状的东西就可以把你卡成 O(n2)O(n^2) 的?!

此时,你突然意识到(怎么做到的?)你想要使树尽量平衡,然后找重心就好了?时间复杂度变成了 O(nlog⁡n)O(n\log n) 的了。(参考一下 lca 的 一种基于错误的寻找重心方法的点分治的复杂度分析)

然后就结束了,好强的丫子,再理一遍过程(打*的为本题过程,不属于框架中):

  1. 找到全树的重心
  2. 开始以重心为根 DFS
  3. *计算每个点的距离
  4. *根据 d−curd-cur 算出是否有合法的
  5. 算出子树的重心
  6. 在子树里 DFS
C++
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define per(i, n) for(int i = (n); i >= 0; --i)
#define rep1(i, n) for(int i = 1; i <= (n); ++i)
#define per1(i, n) for(int i = (n); i >= 1; --i)
#define REP(i, a, b) for(int i = (a); i <= (b); ++i)
#define PER(i, a, b) for(int i = (a); i >= (b); --i)
#define debugv(x) cout << #x << " = " << x << ", line " << __LINE__ << endl
#define debug cout << "at line " << __LINE__ << " in function " << __FUNCTION__ << endl
#define pb push_back
#define mp make_pair
#define nextp next_permutation
#define pq priority_queue
#define fi first
#define se second
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
const int mod1 = 998244353;
const int mod2 = 1000000007;

const int maxn = 1e4 + 10, maxm = 1e7 + 10;
int n, m, q[110], sum, rt;
vector<pii> g[maxn];
int maxx[maxn], siz[maxn],dis[maxn];
bool vis[maxn], app[maxm], ans[110];

void findcenter(int x, int p) {
	siz[x] = 1;
	maxx[x] = 0;
	for(int i = 0; i < (int)g[x].size(); i++) {
		int to = g[x][i].fi;
		if(to == p || vis[to]) continue;
		findcenter(to, x);
		siz[x] += siz[to];
		maxx[x] = max(maxx[x], siz[to]);
	}
	maxx[x] = max(maxx[x], sum - siz[x]);
	if(maxx[x] < maxx[rt]) rt = x;
}

int dd[maxn], tot;

void calcdis(int x, int p) {
	dd[++tot] = dis[x];
	for(int i = 0; i < (int)g[x].size(); i++) {
		int to = g[x][i].fi, w = g[x][i].se;
		if(to == p || vis[to]) continue;
		dis[to] = dis[x] + w;
		calcdis(to, x);
	}
}

queue<int> tag;

void dfs(int x, int p) {
	vis[x] = true;
	app[0] = true;
	tag.push(0);
	for(int i = 0; i < (int)g[x].size(); i++) {
		int to = g[x][i].fi, w = g[x][i].se;
		if(to == p || vis[to]) continue;
		tot = 0;
		dis[to] = w;
		calcdis(to, x);
		for(int j = 1; j <= tot; j++) {
			for(int k = 1; k <= m; k++) {
				if(q[k] >= dd[j]) {
					ans[k] |= app[q[k] - dd[j]];
				}
			}
		}
		for(int j = 1; j <= tot; j++) {
			if(dd[j] <= 1e7) {
				tag.push(dd[j]);
				app[dd[j]] = true;
			}
		}
	}
	while(!tag.empty()) {
		app[tag.front()] = false;
		tag.pop();
	}
	for(int i = 0; i < (int)g[x].size(); i++) {
		int to = g[x][i].fi;
		if(to == p || vis[to]) continue;
		sum = siz[to];
		rt = 0;
		maxx[rt] = 2e9;
		findcenter(to, x);
		findcenter(rt, -1);
		dfs(rt, x);
	}
}

int main() {
	scanf("%d %d", &n, &m);
	int u, v, w;
	for(int i = 1; i < n; i++) {
		scanf("%d %d %d", &u, &v, &w);
		g[u].pb(mp(v, w)); g[v].pb(mp(u, w));
	}
	for(int i = 1; i <= m; i++) scanf("%d", q + i);
	rt = 0;
	maxx[rt] = 2e9;
	sum = n;
	findcenter(1, -1);
	findcenter(rt, -1);
	dfs(rt, -1);
	for(int i = 1; i <= m; i++) {
		if(ans[i]) printf("AYE\n");
		else printf("NAY\n");
	}
	return 0;
}

/*
author: Jerry_Jiang
date: 2022.10.3
problem: Luogu P3806 【模板】点分治1
---------- 点分治模板 -----------
start coding at 18:28
finish coding at 18:47
finish debugging at 23:24
*/
原文