TREESUM

题目大意

给定一棵 \(n\) 个结点的树,定义两点之间距离为两点间简单路径的边数,求树上的点两两之间的距离之和。即求

\[\sum^{n}_{x=1}\sum^n_{y=x+1}\operatorname{dist}(x,y)^k \]

思路

首先考虑把 \(k\) 次方转化掉,利用第二类斯特林数的性质

\[n^k=\sum^{\min(n,k)}_{i=1}\dbinom{n}{i}\times i! \times {k \brace i} \]

考虑其组合意义,等式左边可以理解为将 \(k\) 个球放入 \(n\) 个盒子的方案数,对于每个球有 \(n\) 种可能,盒子与球都是有差别的。

等式右边我们理解为枚举盒子,从 \(n\) 个盒子中选 \(i\) 个,然后将 \(k\) 个球划分为无差别的 \(i\) 个集合,所以我们要乘上 \(i!\) 把盒子的差别补上。

所以我们可以把原式化简,设 \(S(u)\)\(u\) 到其他点的距离的 \(k\) 次幂之和,

\[\begin{aligned} S(u) &= \sum^{n}_{v=1}\operatorname{dist}(u,v)^k \\ &= \sum^{n}_{v=1}\sum^{k}_{i=1}{\operatorname{dist}(u,v) \choose i} \times i! \times {k \brace i} \\ &= \sum^{k}_{i=1} i! \times {k \brace i} \sum^{n}_{v=1}{\operatorname{dist}(u,v) \choose i} \\ \end{aligned} \]

\(dp(u,t) = \displaystyle\sum^n\limits_{v=1} \dbinom{\operatorname{dist}(u,v)}{t}\),我们分子树内和子树外两部分处理。

\(f(u,t)\) 表示以 \(u\) 为根的子树内的 \(\dbinom{\operatorname{dist}(u,v)}{t}\) 之和,\(\operatorname{g}(u,t)\) 表示不在以 \(u\) 为根的子树内的 \(\dbinom{\operatorname{dist}(u,v)}{t}\)

温馨提示,以下篇幅大量用到帕斯卡法则:

\[{n \choose m}={n - 1 \choose m} + {n - 1 \choose m - 1} \]


首先考虑求出子树内部分的贡献,对于 \(x \in \operatorname{son}_u,v \in \operatorname{subtree}_x\),显然有 \(\operatorname{dist}(u,v) = \operatorname{dist}(x,v) + 1\),那么有

\[\begin{aligned} f(u,t) &= \sum_{v \in \operatorname{subtree}_u}{\operatorname{dist}(u,v) \choose t} \\ &= \sum_{x \in \operatorname{son}_u}\sum_{v \in \operatorname{subtree}_x}{\operatorname{dist}(u,v) \choose t} \\ &= \sum_{x \in \operatorname{son}_u}\sum_{v \in \operatorname{subtree}_x}{\operatorname{dist}(x,v) + 1 \choose t} \\ &= \sum_{x \in \operatorname{son}_u}\sum_{v \in \operatorname{subtree}_x}{\operatorname{dist}(x,v) \choose t} +{\operatorname{dist}(x,v) \choose t - 1} \\ &= \sum_{x \in \operatorname{son}_u}f(x,t)+f(x,t - 1) \end{aligned} \]

显然对于 \(x \in \operatorname{son}_u,v \in \operatorname{subtree}_x\),有 \(\operatorname{dist}(x,v)=\operatorname{dist}(u,v) - 1\);对于 \(x \in \operatorname{son}_u,v \notin \operatorname{subtree}_x\),有 \(\operatorname{dist}(x,v)=\operatorname{dist}(u,v) + 1\)

那我们现在可以处理子树内和子树外贡献的和了,考虑设 \(fa\)\(u\) 的父亲。

\[\begin{aligned} dp(u,t) &= f(u,t)+g(u,t) \\ &= \sum_{v \in \operatorname{subtree}_u} {\operatorname{dist}(u,v) \choose t} + \sum_{v \notin \operatorname{subtree}_u} {\operatorname{dist}(u,v) \choose t} \\ &= \sum_{v \in \operatorname{subtree}_u} {\operatorname{dist}(fa,v) - 1 \choose t} + \sum_{v \notin \operatorname{subtree}_u} {\operatorname{dist}(fa,v) + 1 \choose t} \\ &= \sum_{v \in \operatorname{subtree}_u} {\operatorname{dist}(fa,v) \choose t} - \sum_{v \in \operatorname{subtree}_u} {\operatorname{dist}(fa,v)-1 \choose t-1} + \sum_{v \notin \operatorname{subtree}_u} {\operatorname{dist}(fa,v) \choose t} + \sum_{v \notin \operatorname{subtree}_u} {\operatorname{dist}(fa,v) \choose t-1} \\ &= \sum_{v \in \operatorname{subtree}_u} {\operatorname{dist}(fa,v) \choose t} + \sum_{v \notin \operatorname{subtree}_u} {\operatorname{dist}(fa,v) \choose t} - \sum_{v \in \operatorname{subtree}_u} {\operatorname{dist}(fa,v)-1 \choose t-1} + \sum_{v \notin \operatorname{subtree}_u} {\operatorname{dist}(fa,v) \choose t-1} \\ &= \sum_{v \in \operatorname{subtree}_u} {\operatorname{dist}(fa,v) \choose t} + \sum_{v \notin \operatorname{subtree}_u} {\operatorname{dist}(fa,v) \choose t} + \sum_{v \in \operatorname{subtree}_u} {\operatorname{dist}(fa,v) \choose t -1} + \sum_{v \notin \operatorname{subtree}_u} {\operatorname{dist}(fa,v) \choose t-1} - \sum_{v \in \operatorname{subtree}_u} {\operatorname{dist}(fa,v) \choose t -1} - \sum_{v \in \operatorname{subtree}_u} {\operatorname{dist}(fa,v)-1 \choose t-1} \\ &= dp(fa,t)+dp(fa,t - 1) - \sum_{v \in \operatorname{subtree}_u} {\operatorname{dist}(fa,v) \choose t -1} - \sum_{v \in \operatorname{subtree}_u} {\operatorname{dist}(fa,v)-1 \choose t-1} \\ &= dp(fa,t)+dp(fa,t - 1) - \sum_{v \in \operatorname{subtree}_u} {\operatorname{dist}(fa,v)-1 \choose t-1} - \sum_{v \in \operatorname{subtree}_u} {\operatorname{dist}(fa,v)-1 \choose t-2} - \sum_{v \in \operatorname{subtree}_u} {\operatorname{dist}(fa,v)-1 \choose t-1} \\ &= dp(fa,t)+dp(fa,t - 1) - \sum_{v \in \operatorname{subtree}_u} {\operatorname{dist}(u,v) \choose t-1} - \sum_{v \in \operatorname{subtree}_u} {\operatorname{dist}(u,v) \choose t-2} - \sum_{v \in \operatorname{subtree}_u} {\operatorname{dist}(u,v) \choose t-1} \\ &= dp(fa,t)+dp(fa,t - 1)-2f(u,t-1)-f(u,t-2) \\ \end{aligned} \]

那我们就可以递推预处理斯特林数和阶乘,之后换根 DP 求解。

posted @ 2023-10-07 16:33  -白简-  阅读(22)  评论(0编辑  收藏  举报