题解 P4178 【Tree】
Nickel_Angel · · 题解
这里要介绍一下不依靠容斥来避免点分治统计答案重复的方法
(下面简略介绍一下点分治算法的主要思想QwQ)
这个算法主要采用分治的思想:首先在树中选取一个节点作为树根,树根将这棵树分成了几个子树,我们统计这棵树树根对答案的贡献后,我们再用同样的方法递归处理树根的每一颗子树。为了使树分治出的子树尽量平均,尽可能最小化递归层数,我们每次选取当前树的重心作为根节点递归下去,保证递归层数在
本题要统计的是树上的路径,考虑树上路径分为经过根节点和不经过根节点两类。
故可以考虑在分治过程中,统计当前子树经过根节点的路径中,长度小于等于
但这样有可能会统计到一些不符合要求的点对,即
现在正题开始!qwq
看到大家一般都使用的是容斥法处理的以上情况,即每次在统计当前子树根节点的答案后,再减去根节点的各个子节点的子树中满足条件的点对。这样可能会导致常数较大, 故这里介绍另外一种常数较小但细节相对较多的方法:染色法。
考虑我们在一次分治一个子树过程中,我们已经找到了其根节点
有一个显然的性质是,一条路径经过
我们先将树中的节点都取出放至数组中,按照它们到根节点的距离(
具体计算过程如下:
1、将左指针从左向右扫描,对每个点计算合法的路径条数。不难发现,在左指针扫描过程中,恰好使得
2、对于如何去掉不合法的点对,我们可以维护一个数组
3、设当前左指针指向的点为
显然上述排序时间复杂度为
综上,结合 master 定理,本算法时间复杂度为
Code:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
namespace Primary
{
const int maxn = 40010;
int n, m, dp[maxn], size[maxn], dis[maxn], help[maxn];
int head[maxn], to[maxn << 1], next[maxn << 1], val[maxn << 1], tot = 1;
int root, sum, cnt, ans = 0, color[maxn], f[maxn];
bool vis[maxn];
bool cmp(int x, int y) {return dis[x] < dis[y];}
inline void add_edge(int u, int v, int w)
{
to[++tot] = v;
val[tot] = w;
next[tot] = head[u];
head[u] = tot;
}
void getroot(int u, int pre)//树形 dp 求树的重心
{
size[u] = 1, dp[u] = 0;
for (int c = head[u], v; c; c = next[c])
{
v = to[c];
if (v == pre || vis[v]) continue;
getroot(v, u);
size[u] += size[v];
dp[u] = max(dp[u], size[v]);
}
dp[u] = max(dp[u], sum - size[u]);
if (dp[u] < dp[root]) root = u;
}
void getdis(int u, int pre)//对于当前子树预处理 dis, color, f 等数组
{
help[++cnt] = u;//将当前子树节点编号均放入 help 数组中
color[u] = color[pre], ++f[color[u]];
for (int c = head[u], v; c; c = next[c])
{
v = to[c];
if (v == pre || vis[v]) continue;
dis[v] = dis[u] + val[c];
getdis(v, u);
}
}
inline int calc(int u)
{
dis[u] = 0, cnt = 0;
help[++cnt] = u;
color[u] = u, f[color[u]] = 1;//注意预处理细节
for (int c = head[u], v; c; c = next[c])
{
v = to[c];
if (vis[v]) continue;
color[v] = v, dis[v] = val[c];
f[color[v]] = 0;//注意清空 f 数组
getdis(v, v);
}
sort(help + 1, help + cnt + 1, cmp);//将节点按 dis 从小到大排序
int i = 1, j = cnt, res = 0;
while (i < j)
{
while (i < j && dis[help[i]] + dis[help[j]] <= m)
{
res += j - i + 1 - f[color[help[i]]];
--f[color[help[i]]];//指针移动时直接维护 f 数组,有些类似于莫队
++i;
}
--f[color[help[j]]];
--j;
}
return res;
}
void solve(int u)//主要分治函数
{
vis[u] = true;
ans += calc(u);
for (int c = head[u], v; c; c = next[c])
{
v = to[c];
if (vis[v]) continue;
sum = size[v] > size[u] ? n - size[u] : size[v];//计算下层递归子树大小
root = 0, dp[0] = n;
getroot(v, u);//每次求出重心后继续分治
solve(root);
}
}
void main()
{
scanf("%d", &n);
for (int i = 1, u, v, w; i < n; ++i)
{
scanf("%d%d%d", &u, &v, &w);
add_edge(u, v, w), add_edge(v, u, w);
}
scanf("%d", &m);
dp[0] = sum = n;
getroot(1, 0);
solve(root);
printf("%d\n", ans);
}
}
int main()
{
Primary::main();
return 0;
}